2

When I load up the server at example.org(not actual ip) via nginx, the contents show up fine. However, the static files such as css and js doesn't show up. What needs to be changed for static files to be shown? I am just doing python manage.py runserver on nginx to check if files are being fed, but only template show up without static files.

The actual static folder(css/js/img) is at

/opt/myenv/myproject/myproject/static

I have /etc/nginx/sites-available/myproject as

server {
    listen 80;
    server_name example.org; ##not actual ip
    access_log /var/log/nginx/example.log;

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

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

This is what I have for /opt/myenv/myproject/mysite/settings.py.

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

2 Answers2

2

In your question you state that your static resource files are located at /opt/myenv/myproject/myproject/static but your nginx configuration file places them at /opt/myenv/static.

Also, you use alias where root would be more efficient. Try:

location /static {
    root /opt/myenv/myproject/myproject;
}
Richard Smith
  • 45,711
  • 6
  • 82
  • 81
1

Did you collect the static files using the command?:

python manage.py collectstatic

I think also that there is an error in the path you defined for the static's folder, shouldn't it be:

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

Instead of:

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

?

ddalu5
  • 401
  • 4
  • 17