django+gunicorn+nginx wierdness

余生颓废 提交于 2019-12-08 12:37:20

问题


I am following the instructions in http://senko.net/en/django-nginx-gunicorn/ to deploy my django application. My application is running under gunicorn and I have configured nginx to serve the site. Everything is fine except that the static files are looked inside a 'static' subdirectory of the actual static file directory.

I have the following stanza for static files in the nginx configuration file:

location /static {
    root /webapps/myproject/project_staticfiles;
}

where /webapps/myproject/project_staticfiles is the place where I have all the static files (I use manage.py collectstatic to put the static files there). However, they are not found when the site is accessed at the port configured in nginx configuration. The error.log shows that they are looked in a directory deeper: /webapps/myproject/project_staticfiles/static.

Indeed, when I create a new directory 'static' and put all the static files in there, everything is found. In my project settings.py file I have:

STATIC_URL = '/static/'
STATIC_ROOT = '/webapps/myproject/project_staticfiles'

What is the setting that causes this strange behaviour?


回答1:


I think you are looking to use alias:

location /static {
    alias /webapps/myproject/project_staticfiles;
}

If you read the docs on root, it states:

root specifies the document root for the requests. For example, with this configuration:

location  /i/ {   
    root  /spool/w3; 
} 

A request for "/i/top.gif" will return the file "/spool/w3/i/top.gif". You can use variables in the argument.

root appends the directory into the filepath, alias does not, that is why, static gets appended to your root to form /webapps/myproject/project_staticfiles/static



来源:https://stackoverflow.com/questions/15796626/djangogunicornnginx-wierdness

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