通过 nginx 反向代理上传文件失败并超时

通过 nginx 反向代理上传文件失败并超时

小文件(小于 20kb)上传没有问题,任何更大的文件都会导致达到超时限制并出现 504 响应。nginx 配置具有足够大的客户端最大主体大小,超时值不会改变任何东西,即使设置为非常大的值。

        include                       /etc/nginx/mime.types;
        default_type                  application/octet-stream;
        sendfile                      on;
        server_tokens                 off;
        tcp_nopush                    on;
        tcp_nodelay                   on;
        client_body_temp_path         /tmp/nginx/body 1 2;
        keepalive_timeout             300s;
        proxy_connect_timeout         300s;
        proxy_send_timeout            300s;
        proxy_read_timeout            300s;
        ssl_prefer_server_ciphers     on;
        gzip                          on;
        proxy_ignore_client_abort     off;
        client_max_body_size          1000M;
        server_names_hash_bucket_size 1024;
        proxy_http_version            1.1;
        proxy_set_header              X-Forwarded-Scheme $scheme;
        proxy_set_header              X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header              Accept-Encoding "";
        proxy_cache                   off;
        proxy_cache_path              /var/lib/nginx/cache/public  levels=1:2 keys_zone=public-cache:30m max_size=192m;
        proxy_cache_path              /var/lib/nginx/cache/private levels=1:2 keys_zone=private-cache:5m max_size=1024m;

        log_format proxy '[$time_local] $upstream_cache_status $upstream_status $status - $request_method $scheme $host "$request_uri" [Client $remote_addr] [Length $body_bytes_sent] [Gzip $gzip_ratio] [Sent-to $server] "$http_user_agent" "$http_referer"';
        log_format standard '[$time_local] $status - $request_method $scheme $host "$request_uri" [Client $remote_addr] [Length $body_bytes_sent] [Gzip $gzip_ratio] "$http_user_agent" "$http_referer"';

        access_log /data/logs/fallback_access.log proxy;

        # Default upstream scheme
        map $host $forward_scheme {
                default http;
        }

        # Real IP Determination

        # Local subnets:
        set_real_ip_from 10.0.0.0/8;
        set_real_ip_from 172.16.0.0/12;
        set_real_ip_from 192.168.0.0/16;

        include conf.d/include/ip_ranges.conf;

        real_ip_header X-Real-IP;
        real_ip_recursive on;

上传小文件时 POST 成功

[31/Jan/2024:11:20:25 +0000] - 200 200 - POST http website "/up/" [Client [ip]] [Length 37] [Gzip -] [Sent-to 10.0.0.177] "ShareX/15.0.0" "-"

错误日志尾部

2024/01/31 11:25:39 [error] 312417#312417: *510967 upstream timed out (110: Connection timed out) while sending request to upstream, client: [ip], server: website.com, request: "POST /up/ HTTP/1.1", upstream: "http://10.0.0.177:80/up/", host: "website.com"
2024/01/31 11:30:40 [error] 312417#312417: *510967 upstream timed out (110: Connection timed out) while sending request to upstream, client: [ip], server: website.com, request: "POST /up/ HTTP/1.1", upstream: "http://10.0.0.177:80/up/", host: "website.com"

访问日志尾部

[31/Jan/2024:11:25:39 +0000] - 504 504 - POST http website.com "/up/" [Client [ip]] [Length 164] [Gzip -] [Sent-to 10.0.0.177] "ShareX/15.0.0" "-"
[31/Jan/2024:11:30:40 +0000] - 504 504 - POST http website.com "/up/" [Client [ip]] [Length 164] [Gzip -] [Sent-to 10.0.0.177] "ShareX/15.0.0" "-"

相关内容