configurationnginx为django开发静态文件

我正在使用Nginx在django开发服务器上提供静态文件。 但是我不能让它工作。 服务器运行良好,但nginx无法find静态文件。

这是我在nginx.conf中的设置:

listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { #root html; #index index.html index.htm; proxy_pass http://127.0.0.1:8000; proxy_set_header X-Forwarded-Host $server_name; proxy_set_header X-Real-IP $remote_addr; } location /static/ { alias /path/Documents/python_projects/to_pm/static/; access_log off; expires 30d; } 

我的项目settings.py:

 STATIC_URL = '/static/' # for production STATIC_ROOT = os.path.join(BASE_DIR, 'static') 

我也用python manage.py collectstatics把所有静态文件复制到/path/Documents/python_projects/to_pm/static/文件夹。

但是当我使用http://localhost:8080这个站点显得非常难看,显然这个静态文件并不是由nginx提供的。 我怎样才能解决这个问题?

我的错误日志说

13:权限被拒绝“的静态文件

我检查用户,并看到nginx在根目录下作为主进程运行,没有人作为工作进程。 我不能改变用户到root ,我不能改变用户为我的默认用户,因为我得到这个错误,如果我改变用户:

[emerg] getgrnam("root") failed in /usr/local/etc/nginx/nginx.conf:2

我已经find了我的问题的答案:

答案是在这篇文章中:

https://gist.github.com/jhjguxin/6208474

正如它指出的那样:

 Nginx needs to have read permission of the files that should be served AND have execute permission in each of the parent directories along the path from the root to the served files. 

静态文件夹也需要你的nginx用户拥有。

我有这样的一个类似的问题。 事实certificate,这是SELinux更新后显示的问题。

* 如果您的静态文件在用户主文件夹下面,则此解决scheme适用

看看nginxlogin/var/log/nginx/-error.log我可以看到,当尝试打开静态文件时,nginx进程拒绝访问。

之后,我查看了审计日志(/var/log/audit/audit.log)。 这个文件有关于正在发生的所有信息,但很难阅读。

但是有一个很好的工具叫做audit2why( yum install policycoreutils-python

所以如果你试试这个:grep 1433926027.242:416 /var/log/audit/audit.log | audit2why

我有这个号码1433926027.242看着audit.log文件的尾巴

运行时,显示解决scheme。 只需要用setsebool标记一个标志:

setsebool -P httpd_read_user_content 1

之后,我的nginx可以打开没有问题的静态文件。 *

希望能帮助到你!