Ubuntu上Django+Nginx部署环境文件上传故障求助
问题描述
通过Django Admin上传文件时持续触发Nginx警告,最初仅上传大于1MB的文件时出现,现在2KB的小文件也会触发。相关Nginx错误日志如下:
2023/08/24 10:51:09 [warn] 4147#4147: *1 a client request body is buffered to a temporary file /var/cache/nginx/client_temp/0000000001, client: 172.71.98.67, server: 72.167.132.218, request: "POST /ran-admin/blog/post/add/ HTTP/1.1", host: "redaid-nigeria.org", referrer: "https://redaid-nigeria.org/ran-admin/blog/post/add/" 2023/08/24 10:52:02 [error] 4147#4147: *10 open() "/usr/share/nginx/html/server-status" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /server-status?auto HTTP/1.1", host: "localhost" 2023/08/24 10:52:02 [error] 4147#4147: *11 open() "/usr/share/nginx/html/nginx_status" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /nginx_status HTTP/1.1", host: "localhost" 2023/08/24 10:53:28 [warn] 4147#4147: *18 a client request body is buffered to a temporary file /var/cache/nginx/client_temp/0000000002, client: 172.71.98.159, server: 72.167.132.218, request: "POST /ran-admin/blog/post/add/ HTTP/1.1", host: "redaid-nigeria.org", referrer: "https://redaid-nigeria.org/ran-admin/blog/post/add/" 2023/08/24 10:58:06 [warn] 4147#4147: *25 a client request body is buffered to a temporary file /var/cache/nginx/client_temp/0000000003, client: 172.70.46.67, server: 72.167.132.218, request: "POST /ran-admin/blog/post/add/ HTTP/1.1", host: "redaid-nigeria.org", referrer: "https://redaid-nigeria.org/ran-admin/blog/post/add/"
当前Nginx站点配置(/etc/nginx/sites-available/domainwww.conf):
server { listen 80; server_name 72.167.132.218 redaid-nigeria.org www.redaid-nigeria.org; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/ranvps/ranwww; } location / { include proxy_params; proxy_pass http://unix:/run/gunicorn.sock; } }
Nginx主配置(nginx.conf):
user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; 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; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; ## # Virtual Host Configs ## include /etc/nginx/sites-enabled/*; }
已尝试在站点配置和主配置中添加以下参数,但问题未解决:
proxy_request_buffering off; proxy_http_version 1.1; client_body_buffer_size 10M; client_max_body_size 10M; proxy_buffering off;
解决建议
1. 确保缓冲配置在正确作用域生效
Nginx的缓冲参数若在全局http块设置,可能被server或location块的配置覆盖。直接在处理上传请求的location /块中添加配置,确保优先级最高:
location / { include proxy_params; proxy_pass http://unix:/run/gunicorn.sock; # 明确添加缓冲相关配置 client_body_buffer_size 10M; client_max_body_size 10M; proxy_request_buffering off; proxy_buffering off; }
2. 修复Nginx临时目录权限
/var/cache/nginx/client_temp目录权限异常会导致哪怕小文件也触发临时文件写入警告,执行以下命令修复:
sudo chown -R nginx:nginx /var/cache/nginx/ sudo chmod -R 700 /var/cache/nginx/
3. 检查proxy_params文件的冲突
include proxy_params;引入的文件可能包含缓冲相关配置(如proxy_buffering on),覆盖自定义设置。查看该文件内容:
cat /etc/nginx/proxy_params
若存在冲突参数,要么删除该参数,要么在location块中明确设置proxy_buffering off强制覆盖。
4. 配置Django上传限制
Django自身有文件上传大小限制,修改项目settings.py:
# 设置最大内存中存储的上传文件大小(10MB) DATA_UPLOAD_MAX_MEMORY_SIZE = 10 * 1024 * 1024 # 设置临时文件存储目录,确保有读写权限 FILE_UPLOAD_TEMP_DIR = '/tmp'
5. 重启服务生效
修改配置后,重启Nginx和Gunicorn:
sudo systemctl restart nginx sudo systemctl restart gunicorn
6. 处理server-status错误(可选)
日志中的server-status错误是监控工具访问未配置路径导致的,添加以下配置可解决:
server { listen 127.0.0.1:80; server_name localhost; location /server-status { stub_status on; allow 127.0.0.1; deny all; } }
内容的提问来源于stack exchange,提问作者Yinka Ash
相关产品推荐
相关产品推荐

