django - 打开() "/root/project/static/*.css"失败(13 : Permission denied) nginx

标签 django ubuntu nginx permissions django-staticfiles

我必须使用 uwsgi, django, nginx 设置我的项目一切似乎都运行良好,但不知何故,我在获取 static files 时不断收到错误消息我一直在网上阅读并尝试了所有可能的方法,但我不断得到这个permission denied我的静态文件夹上的错误。

有人可以让我知道我在许可方面做错了什么以及我应该如何更改它?

这是我的/var/log/nginx/error.logopen() "/root/project/static/*.css" failed (13: Permission denied), client: 2xx.xx.xx.xxx, server: _, request: "GET /static/*.css HTTP/1.1", host: "1xx.xx.xx.xxx"
这是我的nginx site-available config

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # root /var/www/html;

    # Add index.php to the list if you are using PHP
    # index index.html index.htm index.nginx-debian.html;

    server_name _;

    #location = /favicon.ico { access_log off; log_not_found off; }

    #location /media  {
    #       root /root/project/mediafiles;
    #}


        location ^~ /static/ {
        allow all;  # this is from one of the posts but no luck
        auth_basic off;  # this is from one of the posts but no luck
            root /root/project;
        }


        location / {
            include         uwsgi_params;
            uwsgi_pass      unix:/tmp/uwsgi/project.sock;
        }

}

作为我的文件夹权限

对于项目文件夹,它是 drwxr-xr-x 23 www-data www-data
对于静态文件夹,它是 drwxr-x--- 8 www-data www-data 4096 May 23 14:40 static
我从未允许静态 755也是,但没有运气。
无论如何,这是使用 root 作为用户而不是有一个额外的用户,并且 root 也在 www-data 组中

提前感谢所有帮助。

编辑:

正如所建议的,这是 ps aux | grep nginx 的输出
root       810  0.0  0.0 124972  1440 ?        Ss   02:18   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data   811  0.0  0.0 125688  4840 ?        S    02:18   0:00 nginx: worker process
www-data   812  0.0  0.0 125348  3196 ?        S    02:18   0:00 nginx: worker process
root      1159  0.0  0.0  14224  1004 pts/0    S+   04:25   0:00 grep --color=auto nginx

最佳答案

我猜的问题是您的项目 root目录位于 /root . /root 的默认权限是:
drwx------ 14 root root 4096 May 8 12:47 root
可以看到,其他用户,如www-data甚至没有对 /root 的读取权限目录。在 Linux FS 中,如果您需要读取路径 /a/b/c 处的内容,您需要对该路径中的每个文件夹具有读取权限。

Nginx 工作进程以用户 www-data 身份运行它试图打开一个以 /root 为根的文件该用户没有读取权限,因此引发 Permission denied (13) .

有关更多详细信息,请参阅此演示:

$ cat /root/test2/text.txt     
cat: /root/test2/text.txt: Permission denied
$ sudo cat /root/test2/test.txt  
A
$ sudo ls -la /root/ | grep test2
drwxrwxrwx   2 root     root       4096 May 24 02:04 test2

希望这是有道理的。解决方案将是以下各项:
  • 以 root 身份运行 nginx worker(不推荐)
  • 将您的项目目录移动到设计为可供多个用户访问的位置,例如 /usr/local/share/var/www/ (推荐)
  • 关于django - 打开() "/root/project/static/*.css"失败(13 : Permission denied) nginx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50495218/

    相关文章:

    mysql - 稍后在项目中使用 MySQL 添加模型属性

    python - 在 django 中获取多个站点 id 的问题

    Django 批量更新/插入性能

    apache-flex - 安装 Flex builder 后,Ubuntu 10 上的 Flash 播放器无法正常工作

    vim - Ubuntu、Vim 和曝光调色板

    django - 在 django-rest-framework 中过滤 ListAPIView

    php - 如何修复 "Unable to connect to https://127.0.0.1 SSL certificate problem: self signed certificate"

    macos - 使用 http_perl_module 编译 nginx 1.8.1

    nginx - 重用 nginx.conf 中域的配置语句

    nginx - 我在CSS文件中添加了一行文本,垃圾通过浏览器进入