qwe*_*rty 4 django nginx uwsgi centos7
我有一个网页,Nginx + Uwsgi + Django其中有一个外部路径,用于/download管理 Django 中的下载(用户凭据),以及/download-nginx实际下载目录中文件的内部路径/var/wwww/download。为了进行试验,我尝试使用我的用户名和默认的 nginx 用户来执行此操作。使用这两个方法时,我在 Nginx 上收到权限被拒绝的错误:
open() "/var/www/download/example.txt" failed (13: Permission denied)\nRun Code Online (Sandbox Code Playgroud)\n我已经阅读了其他几个解决方案,告诉问题是 nginx.conf 中提供的用户没有足够的权限。问题是他们确实有足够的权限:
\n$ sudo -u nginx stat /var\n\n File: \xe2\x80\x98/var\xe2\x80\x99\n Size: 4096 Blocks: 8 IO Block: 4096 directory\nDevice: 802h/2050d Inode: 50331745 Links: 21\nAccess: (0777/drwxrwxrwx) Uid: ( 996/ nginx) Gid: ( 0/ root)\nContext: system_u:object_r:var_t:s0\nAccess: 2021-11-23 11:24:53.329927606 +0000\nModify: 2021-11-23 09:43:29.250244353 +0000\nChange: 2021-11-23 11:21:37.151148760 +0000\nRun Code Online (Sandbox Code Playgroud)\n另外,以防万一我chmod 777对目录进行了递归操作/var/wwww/download
我的nginx.conf文件如下:
user nginx;\nworker_processes auto;\nerror_log /var/log/nginx/error.log;\npid /run/nginx.pid;\n\n# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.\ninclude /usr/share/nginx/modules/*.conf;\n\nevents {\n worker_connections 1024;\n}\n\nhttp {\n log_format main '$remote_addr - $remote_user [$time_local] "$request" '\n '$status $body_bytes_sent "$http_referer" '\n '"$http_user_agent" "$http_x_forwarded_for"';\n\n access_log /var/log/nginx/access.log main;\n\n sendfile on;\n tcp_nopush on;\n tcp_nodelay on;\n keepalive_timeout 65;\n types_hash_max_size 4096;\n\n include /etc/nginx/mime.types;\n default_type application/octet-stream;\n\n client_max_body_size 128M;\n proxy_max_temp_file_size 0;\n proxy_buffering off;\n server_names_hash_bucket_size 256;\n\n # Load modular configuration files from the /etc/nginx/conf.d directory.\n # See http://nginx.org/en/docs/ngx_core_module.html#include\n # for more information.\n include /etc/nginx/conf.d/*.conf;\n\n upstream django {\n server 127.0.0.1:8000;\n }\n\n server {\n listen 80;\n listen [::]:80;\n server_name _;\n root /usr/share/nginx/html;\n\n # Load configuration files for the default server block.\n include /etc/nginx/default.d/*.conf;\n\n location /download-nginx {\n internal;\n alias /var/www/download;\n sendfile on;\n proxy_max_temp_file_size 0;\n }\n\n location / {\n uwsgi_pass django;\n proxy_read_timeout 300s;\n proxy_connect_timeout 75s;\n uwsgi_param Host $host;\n uwsgi_param X-Real-IP $remote_addr;\n uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;\n uwsgi_param X-Forwarded-Proto $http_x_forwarded_proto;\n\n uwsgi_param QUERY_STRING $query_string;\n uwsgi_param REQUEST_METHOD $request_method;\n uwsgi_param CONTENT_TYPE $content_type;\n uwsgi_param CONTENT_LENGTH $content_length;\n uwsgi_param REQUEST_URI $request_uri;\n uwsgi_param PATH_INFO $document_uri;\n uwsgi_param DOCUMENT_ROOT $document_root;\n uwsgi_param SERVER_PROTOCOL $server_protocol;\n uwsgi_param HTTPS $https if_not_empty;\n uwsgi_param REMOTE_ADDR $remote_addr;\n uwsgi_param REMOTE_PORT $remote_port;\n uwsgi_param SERVER_PORT $server_port;\n uwsgi_param SERVER_NAME $server_name;\n }\n\n error_page 404 /404.html;\n location = /404.html {\n }\n\n error_page 500 502 503 504 /50x.html;\n location = /50x.html {\n }\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n我的 Django 网页上的下载视图如下所示(尽管我很确定错误不在该代码片段中):
\ndef download(request):\n # Auth code is ommitted #\n response = HttpResponse()\n path = "/var/www/download/example.txt"\n name = "example.txt"\n response['Content-Length'] = os.path.getsize(path)\n response['X-Accel-Redirect'] = "/download-nginx/{0}".format(name)\n del response['Content-Type']\n del response['Content-Disposition']\n del response['Accept-Ranges']\n del response['Set-Cookie']\n del response['Cache-Control']\n del response['Expires']\n return response\nRun Code Online (Sandbox Code Playgroud)\n因此,我的问题是:我应该在我的 Centos 机器上做什么,以便能够访问数据并将/var/www/download其作为可下载元素提供给用户?
问题已解决:Nginx 需要每个目录的 +x 权限。这是通过以下方法解决的:
sudo chmod +x /var
sudo chmod +x /var/www
sudo chmod +x /var/www/download
Run Code Online (Sandbox Code Playgroud)