Nginx 在 Digital Ocean 上的 Django 应用程序中访问静态文件时引发 403 Forbidden

Bri*_*bot 5 django nginx django-staticfiles

在我的系统上完成了本地开发的 django 项目后,我按照 Digital Ocean 教程在此处部署了 django 应用程序Digital Ocean Django ASGI 设置教程

我根据教程配置了gunicorn,并且所有测试和确认都通过了但是当我从其公共IP地址加载我的网站时,该网站加载时没有任何样式或设计,也没有图像......就像纯文本一样(只是html内容)

我的nginx配置如下

server {
    listen 80;
    server_name XXX.XX.XXX.XX;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        alias /home/username/projectdir/staticfiles;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的 django 项目设置如下(有关静态设置的部分)

# The absolute path to the directory where collectstatic will collect static files for deployment.
STATIC_ROOT  = BASE_DIR / 'staticfiles'

# The URL to use when referring to static files (where they will be served from)
STATIC_URL = '/static/'

# extra places to find static filed outside static folder inside installed apps
STATICFILES_DIRS = [ BASE_DIR/"static" ]

MEDIA_ROOT = BASE_DIR/"media"
Run Code Online (Sandbox Code Playgroud)

可以提出任何解决方案吗?我已经看到所有其他相关的 stackoverflow 问题也有类似的问题,但他们的解决方案对我不起作用。提前致谢

小智 6

这看起来像是一个权限问题。settings.py我遇到了同样的问题,我通过向和 中指定的静态文件夹授予正确的权限来修复它nginx.conf

此外,在 中nginx.conf,我user通过添加我当前的用户名 + 我的组来编辑该行。以前,我有:user nginx; 我把它改为:user <my_username> <group>

您可以通过在终端中username运行来获取。whoami您可以用来id -g -n $whoami查找您的群组。

希望能帮助到你。