如何修复生产中的 502 Bad Gateway 错误(Nginx)?

Ato*_*ore 6 python django nginx gunicorn nginx-config

当我尝试在数字海洋中托管的项目中上传大小约为 600MB 的大 csv 文件时,它尝试上传但显示 502 Bad Gateway Error (Nginx)。

该应用程序是一个数据转换应用程序。

这在本地工作时效果很好。

sudo tail -30 /var/log/nginx/error.log
Run Code Online (Sandbox Code Playgroud)

节目

[error] 132235#132235: *239 upstream prematurely closed connection while reading response header from upstream, client: client's ip , server: ip, request: "POST /submit/ HTTP/1.1", upstream: "http://unix:/run/gunicorn.sock:/submit/", host: "ip", referrer: "http://ip/"

sudo nano /etc/nginx/sites-available/myproject
Run Code Online (Sandbox Code Playgroud)

节目

server {
    listen 80;
    server_name ip;
    client_max_body_size 999M;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
       alias  /root/static/;
    }
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}
Run Code Online (Sandbox Code Playgroud)

nginx.conf

user root;
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;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;
Run Code Online (Sandbox Code Playgroud)

在转换过程发生时,我还运行了 javascript 加载程序。我怎样才能解决这个问题?

Che*_* A. 1

此错误可能表明存在多个问题。它在本地适用的事实增加了问题依赖于 nginx 端的可能性。

您可以尝试通过增加超时阈值(如此处建议的和缓冲区大小来解决该问题。将其添加到服务器的 nginx.conf 中:

proxy_read_timeout 300s;
proxy_connect_timeout 300s;

proxy_buffer_size          128k;
proxy_buffers              4 256k;
proxy_busy_buffers_size    256k;
Run Code Online (Sandbox Code Playgroud)