Django+Nginx+Gunicorn图片处理报错:上游连接提前关闭
使用Django框架,排除Django代码问题。通过Model Form上传约500KB图片,用OpenCV转灰度图(后续还有更多操作)。Nginx+Gunicorn部署,除文件上传编辑外其他功能正常。
报错信息
upstream prematurely closed connection while reading response header from upstream,客户端:174...194,服务器:.com,请求:"POST / HTTP/1.1",上游:"http://unix:/run/gunicorn.sock:/",主机:".com",来源:"http://****.com/"
配置文件
/etc/systemd/system/gunicorn.service
[Unit] Description=gunicorn daemon Requires=gunicorn.socket After=network.target [Service] User=shubham Group=www-data WorkingDirectory=/home/shubham/editor/src ExecStart=/home/shubham/editor/venv/bin/gunicorn \ --access-logfile - \ --workers 3 \ --timeout 600 \ --bind unix:/run/gunicorn.sock \ src.wsgi:application Restart=always RestartSec=3 [Install] WantedBy=multi-user.target
/etc/systemd/system/gunicorn.socket
[Unit] Description=gunicorn socket [Socket] ListenStream=/run/gunicorn.sock [Install] WantedBy=sockets.target
/etc/nginx/nginx.conf
user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; client_max_body_size 10000M; proxy_connect_timeout 300s; proxy_read_timeout 300s; client_body_buffer_size 10000M; proxy_max_temp_file_size 10000M; send_timeout 300s; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript... ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #}
/etc/nginx/sites-available/editor
server { listen 80; server_name ****.com; client_max_body_size 10000M; proxy_max_temp_file_size 10000M; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/shubham/editor/src/static; } location /media/ { root /home/shubham/editor/src/media; } location / { include proxy_params; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_read_timeout 300s; proxy_connect_timeout 75s; client_max_body_size 10000M; client_body_buffer_size 10000M; proxy_max_temp_file_size 10000M; send_timeout 300s; proxy_pass http://unix:/run/gunicorn.sock; } }
错误日志(/var/log/nginx/error.log)
2023/02/03 06:29:27 [error] 2479#2479: *1 upstream prematurely closed connection while reading response header from upstream, client: 174.***.***.194, server: ****.com, request: "POST / HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/", host: "****.com", referrer: "http://****.com/"
补全Gunicorn错误日志:当前仅配置了访问日志,未记录错误。修改
gunicorn.service的ExecStart行,添加错误日志配置:ExecStart=/home/shubham/editor/venv/bin/gunicorn \ --access-logfile - \ --error-logfile /var/log/gunicorn/error.log \ --workers 3 \ --timeout 600 \ --bind unix:/run/gunicorn.sock \ src.wsgi:application先创建日志目录并设置权限:
sudo mkdir -p /var/log/gunicorn sudo chown shubham:www-data /var/log/gunicorn重启Gunicorn后查看错误日志,能直接定位进程崩溃原因。
手动测试OpenCV逻辑:在虚拟环境中启动Django shell,复现图片处理流程:
cd /home/shubham/editor/src ../venv/bin/python manage.py shell执行测试代码:
import cv2 img = cv2.imread('/path/to/your-test-image.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.imwrite('/tmp/gray-test.jpg', gray)若执行失败,会直接抛出错误(比如依赖缺失、文件权限不足)。
排查系统OOM问题:如果OpenCV处理时占用内存过高,系统会直接杀掉Gunicorn进程。检查系统日志:
sudo dmesg | grep -i kill若发现OOM记录,要么优化图片处理逻辑(比如先缩小尺寸),要么升级服务器内存,或给Gunicorn添加内存限制(在
gunicorn.service的[Service]块添加MemoryLimit=2G)。修复文件权限:确保媒体目录权限正确,Gunicorn用户有读写权限:
sudo chown -R shubham:www-data /home/shubham/editor/src/media sudo chmod -R 755 /home/shubham/editor/src/media简化Nginx缓冲区配置:当前
client_body_buffer_size设为10000M不合理,远超系统内存。修改nginx.conf和站点配置中的该参数为合理值,比如16M,保留client_max_body_size 100M即可满足需求。修改后重启Nginx:sudo systemctl restart nginx
内容的提问来源于stack exchange,提问作者AgentNirmites

