【问题标题】:open() "/root/project/static/*.css" failed (13: Permission denied) nginxopen() "/root/project/static/*.css" 失败 (13: Permission denied) nginx
【发布时间】:2018-11-02 20:20:20
【问题描述】:

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

谁能告诉我我在权限方面做错了什么以及我应该如何更改它?

这是我的/var/log/nginx/error.log

open() "/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

【问题讨论】:

  • nginx 是以哪个用户身份运行的?做一个ps aux | grep nginx并分享请
  • @rtindru 添加了您的建议
  • 已回复;如果可行,请 lmk

标签: django ubuntu nginx permissions django-staticfiles


【解决方案1】:

我也有同样的问题。我在 Centos 7.6 上的 nginx 服务器无法访问路径 /home/user/app/mysyte/static/ 中的静态文件夹。在/var/log/nginx/error.log同样的错误open() "/home/user/app/mysyte/static/*.css" failed (13: Permission denied)

要解决这个问题,请查看page issue 2

【讨论】:

    【解决方案2】:

    我猜测的问题是您的项目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/(推荐)

    【讨论】:

      猜你喜欢
      • 2021-11-08
      • 1970-01-01
      • 2019-12-17
      • 2017-06-18
      • 2015-10-22
      • 2021-01-31
      • 2022-01-02
      • 2016-06-06
      • 1970-01-01
      相关资源
      最近更新 更多