flask – nginx occasionally 502 “upstream prematurely closed connection while reading response header from upstream”

I have a uWSGI server (flask apps):

client -> nginx -> clb -> uwsgi -> python flask

Occasionally, the nginx access.log records 502 status code:

[30.45.200.183 -> 30.45.192.52:8081] [30/Aug/2023:04:55:39 +0800] [502] [502] [GET /backend/change-check/task/result?task_id=596764 HTTP/1.1] [166 Byte] [referer: -] [agent: ReactorNetty/1.0.13] [forward: -] [time_all: 0.014] [upstream_time: 0.014]

At the same time the nginx error.log records:

upstream prematurely closed connection while reading response header from upstream

The tcpdump records the server return FIN without http ok response:

enter image description here

Normally there should be a http response:

enter image description here

I wonder why the server returns FIN without http response, whether is this the reason of occasional 502 status?

Read more here: Source link