Nginx下载权限错误 - Open()失败(13:权限被拒绝)

4
我有一个网页,使用 Nginx + Uwsgi + Django 技术,并且我在其中设置了一个外部路径 /download 用于管理 Django 中的下载(用户凭证),以及内部路径 /download-nginx 用于实际下载目录 /var/wwww/download 中的文件。为了试验,我尝试使用我的用户名和默认的 nginx 用户进行操作,但两者都在 Nginx 中遇到了权限被拒绝的错误。
open() "/var/www/download/example.txt" failed (13: Permission denied)

我读过 Stack Overflow 上的一些其他解决方案,它们都说问题在于 nginx.conf 中提供的用户没有足够的权限。但事实上,他们已经拥有足够的权限:

$ sudo -u nginx stat /var

  File: ‘/var’
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 802h/2050d      Inode: 50331745    Links: 21
Access: (0777/drwxrwxrwx)  Uid: (  996/   nginx)   Gid: (    0/    root)
Context: system_u:object_r:var_t:s0
Access: 2021-11-23 11:24:53.329927606 +0000
Modify: 2021-11-23 09:43:29.250244353 +0000
Change: 2021-11-23 11:21:37.151148760 +0000

此外,以防万一,我已经对目录/var/wwww/download递归执行了chmod 777操作。

我的nginx.conf文件如下:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    client_max_body_size 128M;
    proxy_max_temp_file_size 0;
    proxy_buffering off;
    server_names_hash_bucket_size 256;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    upstream django {
        server 127.0.0.1:8000;
    }

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

    location /download-nginx {
            internal;
            alias /var/www/download;
            sendfile on;
            proxy_max_temp_file_size 0;
    }

    location / {
        uwsgi_pass django;
        proxy_read_timeout 300s;
        proxy_connect_timeout 75s;
        uwsgi_param Host $host;
        uwsgi_param X-Real-IP $remote_addr;
        uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;
        uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;

        uwsgi_param QUERY_STRING $query_string;
        uwsgi_param REQUEST_METHOD $request_method;
        uwsgi_param CONTENT_TYPE $content_type;
        uwsgi_param CONTENT_LENGTH $content_length;
        uwsgi_param REQUEST_URI $request_uri;
        uwsgi_param PATH_INFO $document_uri;
        uwsgi_param DOCUMENT_ROOT $document_root;
        uwsgi_param SERVER_PROTOCOL $server_protocol;
        uwsgi_param HTTPS $https if_not_empty;
        uwsgi_param REMOTE_ADDR $remote_addr;
        uwsgi_param REMOTE_PORT $remote_port;
        uwsgi_param SERVER_PORT $server_port;
        uwsgi_param SERVER_NAME $server_name;
    }

    error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

}

我的Django网页上的下载视图如下(虽然我很确定错误不在这个片段中):
def download(request):
    # Auth code is ommitted #
    response = HttpResponse()
    path = "/var/www/download/example.txt"
    name = "example.txt"
    response['Content-Length'] = os.path.getsize(path)
    response['X-Accel-Redirect'] = "/download-nginx/{0}".format(name)
    del response['Content-Type']
    del response['Content-Disposition']
    del response['Accept-Ranges']
    del response['Set-Cookie']
    del response['Cache-Control']
    del response['Expires']
    return response

因此,我的问题是:为了能够访问位于/var/www/download上的数据并将其提供给用户作为可下载元素,我应该在我的Centos机器上做什么?
1个回答

8
问题解决:Nginx需要在每个目录上添加+x权限。解决方法如下:
sudo chmod +x /var
sudo chmod +x /var/www
sudo chmod +x /var/www/download

我也注意到了Rocky Linux上的这种奇怪现象,它会导致日志轮换出现问题。可以在这里找到关于目录中x位的相当全面的概述。 - fvu

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