Fetching static files failed with 404 in nginx

别说谁变了你拦得住时间么 提交于 2019-12-03 05:43:53

问题


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?


回答1:


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.




回答2:


TL;DR - you should check your files permissions

Stumbled upon this question and I was already using alias instead of root, so while the existing answer was up to the point, the following could be useful to others.

In my case, the solution to the same symptoms turned out to be the files in static/ having root as owner.

A simple chown www-data:www-data . solved the problem, and no more 404.



来源:https://stackoverflow.com/questions/26237936/fetching-static-files-failed-with-404-in-nginx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!