打开() "/root/project/static/*.css" 失败 (13: 权限被拒绝) nginx

3
我已经使用设置好了项目,一切似乎都正常工作,但是我始终无法获取静态文件,我一直在阅读在线资料并尝试所有可能的方法,但我仍然无法在我的静态文件夹上获得权限被拒绝错误。请问有人能告诉我我在权限方面做错了什么,以及我应该如何更改?这是我的/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

我从未将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
@rtindru 添加了你的建议。 - Dora
已回复,请告诉我这是否可行。 - rtindru
4个回答

13

我猜测的问题是您的项目目录位于/root/root的默认权限为:

drwx------ 14 root root 4096 May 8 12:47 root

可以看到,其他用户(例如www-data)甚至没有读取/root目录的权限。在Linux文件系统中,如果您需要读取路径/a/b/c中的内容,则需要对该路径中的每个文件夹都具有读取权限。

Nginx工作进程以用户www-data身份运行,他试图打开一个文件,这个文件位于根目录/root下,但该用户没有读取权限,因此引发了权限被拒绝(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 工作进程(不推荐)
  • 将您的项目目录移动到一个旨在供多个用户访问的位置,例如 /usr/local/share/var/www/(推荐)

你的回答解决了我长期以来一直存在的问题。谢谢。 - josterricardo
例如/usr/local/share或/var/www/(推荐)-这拯救了我的一天!@rtindru非常感谢!希望在未来几年中遇到此问题的人也能从这个答案中得到正确的解决方案 - Dinesh Sakthivel
非常感谢,将我的项目移动到/usr/local/share或/var/www/,拯救了我的一天。 - Anthony Lauly

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问题2。

0
正如@rtindru所说,解决问题的方法是将项目目录移动到专为多个用户访问而设计的位置,例如/usr/local/share或/var/www/。以下是一个关于如何执行此操作的资源the source
我曾经遇到过类似的静态文件加载问题,这里是我用来更改的命令。
sudo rsync -av /home/ubuntu/MyBlog/firstproject/static /var/www/

其中MyBlog/firstproject是我的项目目录。请记得只更改静态目录。

sudo nano /etc/nginx/sites-enabled/myproject

myproject是为nginx创建的目录。 添加以下行:

server {

        root /var/www/static;
   
        . . .
}

现在按照源建议重新启动Nginx。希望这能起作用。


0

我也遇到了同样的问题并且发现这个答案很有用!

Nginx连接.sock失败(13:权限被拒绝)- 502坏网关

我所做的就是更改/etc/nginx/nginx.conf文件中第一行用户名的名称。

在我的情况下,默认用户为www-data,我将其更改为我的root机器用户名。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接