Python Nginx 对静态文件抛出 403 Forbidden
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20182329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Nginx is throwing an 403 Forbidden on Static Files
提问by CodeTalk
I have a django app, python 2.7 with gunicorn and nginx.
我有一个 django 应用程序,python 2.7 with gunicorn 和 nginx。
Nginx is throwing a 403 Forbidden Error, if I try to view anything in my staticfolder @:
403 Forbidden Error如果我尝试查看static文件夹 @ 中的任何内容,Nginx 会抛出一个:
/home/ubuntu/virtualenv/myapp/myapp/homelaunch/static
nginx config(/etc/nginx/sites-enabled/myapp) contains:
nginx 配置( /etc/nginx/sites-enabled/myapp) 包含:
server {
listen 80;
server_name *.myapp.com;
access_log /home/ubuntu/virtualenv/myapp/error/access.log;
error_log /home/ubuntu/virtualenv/myapp/error/error.log warn;
connection_pool_size 2048;
fastcgi_buffer_size 4K;
fastcgi_buffers 64 4k;
root /home/ubuntu/virtualenv/myapp/myapp/homelaunch/;
location /static/ {
alias /home/ubuntu/virtualenv/myapp/myapp/homelaunch/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
error.log contains:
error.log 包含:
2013/11/24 23:00:16 [error] 18243#0: *277 open() "/home/ubuntu/virtualenv/myapp/myapp/homelaunch/static/img/templated/home/img.png" failed (13: Permission denied), client: xx.xx.xxx.xxx, server: *.myapp.com, request: "GET /static/img/templated/home/img2.png HTTP/1.1", host: "myapp.com", referrer: "http://myapp.com/"
access.log contains
access.log 包含
xx.xx.xx.xxx - - [24/Nov/2013:23:02:02 +0000] "GET /static/img/templated/base/animg.png HTTP/1.1" 403 141 "http://myapp.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:25.0) Gecko/20100101 Firefox/25.0"
xx.xx.xx.xxx - - [24/Nov/2013:23:02:07 +0000] "-" 400 0 "-" "-"
I tried just viewing say a .cssfile in /static/and it throws an error like this in source:
我试着只查看说一个.css文件/static/,它在源代码中抛出这样的错误:
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.1.19</center>
</body>
</html>
采纳答案by bryn
It appears the user nginx is running as (nginx?) is missing privileges to read the local file /home/ubuntu/virtualenv/myapp/myapp/homelaunch/static/img/templated/home/img.png. You probably wanna check file permissions as well as permissions on the directories in the hierarchy.
看起来用户 nginx 正在运行,因为(nginx?)缺少读取本地文件的权限/home/ubuntu/virtualenv/myapp/myapp/homelaunch/static/img/templated/home/img.png。您可能想检查文件权限以及层次结构中目录的权限。
回答by eezis
Try specifying a user at the top of your nginx.conf, above the server section.
尝试在 nginx.conf 顶部,服务器部分上方指定一个用户。
user www-data;
回答by CodeTalk
After hours upon hours following so many articles, I ran across : http://nicholasorr.com/blog/2008/07/22/nginx-engine-x-what-a-pain-in-the-bum/
经过这么多文章几个小时后,我遇到了:http: //nicholasorr.com/blog/2008/07/22/nginx-engine-x-what-a-pain-in-the-bum/
which had a comment to chmod the whole django app dir, so I did:
其中对 chmod 整个 django 应用程序目录有评论,所以我做了:
sudo chmod -R myapp
This fixed it. Unbelievable!
这修复了它。逆天!
Thanks to those who offered solutions to fix this.
感谢那些提供解决方案的人。
回答by o_c
The minimum fix that worked for me is:
对我有用的最低修复是:
sudo chmod -R 664 /home/ubuntu/virtualenv/myapp/myapp/homelaunch/static/
sudo chmod -R a+X /home/ubuntu/virtualenv/myapp/myapp/homelaunch/static/
(BTW, in my case the static folder is called collected_static)
(顺便说一句,在我的情况下,静态文件夹被称为collected_static)
回答by Kamil Kie?czewski
MacOs El Capitan: At the top of nginx.confwrite user username group_name
MacOs El Capitan: 在nginx.conf写作的顶部user username group_name
My user name is Kamil so i write:
我的用户名是 Kamil,所以我写:
user Kamil staff;
(word 'staff' is very important in macOS). This do the trick. After that you don't need to change any permission in your project folder and files.
(“员工”一词在 macOS 中非常重要)。这行得通。之后,您无需更改项目文件夹和文件中的任何权限。
回答by user4212639
It seems the web server user doesn't have read permissions to the static files. You can solve this in 2 ways:
Web 服务器用户似乎没有静态文件的读取权限。您可以通过两种方式解决此问题:
(easiest, safer) run the nginx as you app user instead of default
nginxuser. To do this, add the following in nginx.confuser your_app_userReplace
your_app_userwith appropriate unix username for your app. In this case theyour_app_useralready has necessary permissions to the static content.Another way would be to to grant permissions for the web server user to the static dir.
(最简单,更安全)以您的应用程序用户而不是默认
nginx用户身份运行 nginx 。为此,请在nginx.conf 中添加以下内容user your_app_user替换
your_app_user为您的应用程序的适当 unix 用户名。在这种情况下,your_app_user已经拥有对静态内容的必要权限。另一种方法是将 Web 服务器用户的权限授予静态目录。

