Nginx 为 django 静态文件提供 403 代码

Nginx giving 403 code for django static files

我正在尝试部署我的 Django 项目。在我的django设置中,静态文件设置是这样的:

STATIC_URL = '/static/'
STATIC_ROOT = '/root/www/static'

所以我的静态文件在 /root/www/static 目录中,我可以在服务器中看到它们。我的 nginx 服务器块是这样的:

server{
   listen 80;
   location /static/ {
   root /root/www;
   }

   location / {
   proxy_pass http://127.0.0.1:8000;
   proxy_set_header Host $host;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}

当我使用浏览器浏览我的应用程序时,我得到

403 - Forbidden

静态文件错误。

您不应该将静态文件放在 /root 下:那是为了只能通过 root 用户访问的文件。

问题似乎与用户权限有关。您需要更改部署应用程序的用户可访问的路径。

位置应如下所示:

location /static {
        alias /home/ubuntu/srv/webapps/[app_name]/static;
    }

尝试 this 教程以获得更多帮助。