Nginx 分段上传 - 读取上游错误的响应标头时上游服务器暂时被禁用

Nginx 分段上传 - 读取上游错误的响应标头时上游服务器暂时被禁用

我正在从 Android 设备向我的 API 发送分段文件上传。这是我的 nginx 位置/服务器脚本/和错误日志。请帮忙理清。如何处理此分段上传。

 location ^~ /upload/pic/ {
            
            limit_req zone=PERIP; #zone=PERIP:10m rate=1r/s
            proxy_redirect          off;                
            proxy_set_header        Host            $host;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Connection "";    
            proxy_http_version 1.1;
            
            
            client_max_body_size 80M;
            client_body_buffer_size 80M;
            client_body_timeout 120;
            proxy_pass   http://backend;  
            proxy_buffering on;
            proxy_buffer_size 1k;
            proxy_buffers 24 4k;
            proxy_busy_buffers_size 8k;
            proxy_max_temp_file_size 2048m;
            proxy_temp_file_write_size 32k;
            proxy_request_buffering off;
        }
    

Python服务器脚本:

async def upload_pic(self, request):
        async for field in (await request.multipart()):
            if (field.name == 'imagefile') and (is_allowed_imagefile(field.filename)):
                filename = field.filename              
                size = 0   
                with NamedTemporaryFile(mode='w+b', suffix='.jpg') as imagefile:
                    while True:
                        chunk = await field.read_chunk()
                        if not chunk:
                            break
                        size += len(chunk)
                        imagefile.write(chunk)
                    imagefile.flush()
                    imagefile.seek(0)
                    imagefile.read()

......

2个上游服务器的Nginx错误日志:

*10 upstream prematurely closed connection while reading response header from upstream, client: xxx.xxx, server: example.com, request: "POST /upload/pic HTTP/1.1", upstream: "http://unix:/tmp/mapp_1.sock:/upload/pic", host: "example.com"
*10 upstream server temporarily disabled while reading response header from upstream, client: xxx.xxx, server: example.com, request: "POST /upload/pic HTTP/1.1", upstream: "http://unix:/tmp/mapp_1.sock:/upload/pic", host: "example.com"

相关内容