Fetching static files failed with 404 in nginx

Alfred Huang picture Alfred Huang · Oct 7, 2014 · Viewed 18.2k times · Source

I'm now deploying an django app with nginx and gunicorn on ubuntu 12.

And I configure the nginx virtual host file as below:

server {
    listen 80;
    server_name mydomain.com;
    access_log  /var/log/nginx/gunicorn.log;

    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;
    }

    location /static/ {
        root /var/www/django/ecerp/erp/static/;
    }

}

I can request the django well, but when request a static file, it response with 404 status.

I'm sure the root path of static file is correct.

Can anyone help?

Answer

Marc Plano-Lesay picture Marc Plano-Lesay · Oct 7, 2014

You should use alias instead of root. root appends the trailing URL parts to your local path (e.g. http://test.ndd/trailing/part, it will add /trailing/part to your local path). Instead of that, alias does exactly what you want: when http://test.ndd/static/ is requested, /static is mapped to your alias exactly, without appending static again.