0

I'm trying to serve static files with nginx for my Django app.

I'm following this guide in which they propose the following nginx config file:

server {
    server_name yourdomainorip.com;

    access_log off;

    location /static/ {
        alias /opt/myenv/static/;
    }

    location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}

And in my settings.py I have the following declarations:

STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static/')
STATIC_URL = '/static/'
STATIC_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

I'm able to see the static files at urls of the form:

http://x.x.x.x/static/admin/img/icon_searchbox.png

However, when trying to reference static files in my templates using the {% static %} tag, I get urls of the form:

http://x.x.x.x:8001/static/admin/img/icon_searchbox.png

which doesn't point to the files and instead are procesed by Django.

Any ideas of how can I solve this?

2 Answers 2

1

Try this:

STATIC_URL = '/static/'

It was missing the first "/"

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, it was a typo. The first "/" is present in my settings file.
0

Try this config for nginx:

location / {
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.