You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu部署Django多文件上传触发Nginx 502错误的解决求助

问题描述

在DigitalOcean的Ubuntu 18+系统上部署的Django应用,上传5张及以上文件时出现502 Bad Gateway错误(Nginx 1.18.0),错误日志显示:

2022/08/05 11:13:38 [error] 94134#94134: 108 upstream prematurely closed connection while reading response header from upstream, client: 31*...23, server: 15**..2, request: "POST /profil/galrtia/apartment-rent/1/ HTTP/1.1", upstream: "http://unix:/home/app/run/gunicorn.sock:/profil/galrtia/apartment-rent/1/", host: "1..2", referrer: "http://158.**.***82/profil/galrtia/apartment-rent/1/"

上传2-3张照片正常,5张及以上会在3-4秒后报错,已增大Nginx文件限制和超时参数但无效,当前Nginx配置如下:

upstream app_server {
    server unix:/home/app/run/gunicorn.sock fail_timeout=0;
}

server {
    listen 80;

    # add here the ip address of your server
    # or a domain pointing to that ip (like example.com or www.example.com)
    server_name 1**.**.***.**2;

    keepalive_timeout 10000;
    client_max_body_size 10G;

    access_log /home/app/logs/nginx-access.log;
    error_log /home/app/logs/nginx-error.log;

    # Compression config
    gzip on;
    gzip_min_length 1000;
    gzip_buffers 4 32k;
    gzip_proxied any;
    gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    location /static/ {
        alias /home/app/static/;
    }

    location /media/ {
        alias /home/app/app/app/media/;    }

    # checks for static file, if not found proxy to app
    location / {
        try_files $uri @proxy_to_app;
    }

    location @proxy_to_app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app_server;
    }
}
解决方案

1. 调整Gunicorn超时与进程配置

upstream prematurely closed connection核心原因是Gunicorn提前断开了连接,需修改启动参数:

  • 添加超时参数:启动时追加--timeout 120(可根据实际上传时长调整,比如设为120秒)
  • 增加工作进程:服务器资源允许的话,追加--workers 4(一般按CPU核心数*2+1设置)
  • 放宽请求限制:追加--limit-request-line 8192和--limit-request-field_size 8192,避免大请求被截断

修改后重启Gunicorn:

sudo systemctl restart gunicorn

2. 补充Nginx代理超时配置

当前Nginx仅设置了keepalive_timeout,需在location @proxy_to_app块中添加代理相关超时参数:

location @proxy_to_app {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  # 新增代理超时参数
  proxy_connect_timeout 120s;
  proxy_send_timeout 120s;
  proxy_read_timeout 120s;
  proxy_buffering off;  # 禁用缓冲,避免大请求堆积
  proxy_pass http://app_server;
}

修改后校验配置并重启Nginx:

sudo nginx -t && sudo systemctl restart nginx

3. 检查Django上传配置与目录权限

  • 调整settings.py参数:确保DATA_UPLOAD_MAX_MEMORY_SIZE足够大(比如设为10*1024*1024*1024,和Nginx的client_max_body_size匹配);若设置了FILE_UPLOAD_MAX_MEMORY_SIZE,也需同步放宽或注释,让Django自动将大文件写入临时目录。
  • 修复媒体目录权限:确保Gunicorn运行用户拥有读写权限:
sudo chown -R www-data:www-data /home/app/app/app/media/
sudo chmod -R 755 /home/app/app/app/media/

4. 提升系统文件描述符限制

多文件上传会占用大量文件句柄,需放宽系统限制:

  • 临时调整:执行ulimit -n 65535
  • 永久生效:编辑/etc/security/limits.conf,添加:
www-data soft nofile 65535
www-data hard nofile 65535

重启系统或重新登录后生效。

内容的提问来源于stack exchange,提问作者Maddie Graham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 10:15:34