502 Bad Gateway from time to time, and the NGINX logs of the service show this line at the same moment:
What the error means
NGINX forwarded the request to a pod of your service, and the pod closed the TCP connection (it sent a TCP reset) before it returned any response header. NGINX cannot give the client an answer, so it returns502.
The reset comes from the pod side: your application, its runtime, or the pod being stopped. NGINX only reports it. To confirm, open the service Logs in the Console and look at the NGINX logs: the access line for the failed request shows the 502 status and the upstream address of the pod.
Cause 1: the application closes idle connections before NGINX does
This is the most common cause when the errors look random and grow with traffic. NGINX keeps connections to your pods open and reuses them for the next requests. The ingress-nginx controller keeps an idle upstream connection for 60 seconds by default. Many application servers close idle connections much sooner:
When NGINX sends a request on a connection that the application is closing at that very moment, the application answers with a reset and the request fails with
502.
Fix: make the application keep idle connections open longer than NGINX does, so NGINX is always the side that closes them. Set the keep-alive timeout of your server above 60 seconds.
For Node.js, set both timeouts on the HTTP server, with headersTimeout higher than keepAliveTimeout:
app.getHttpServer() after await app.listen(port).
For Gunicorn, start it with --keep-alive 75. For Uvicorn, use --timeout-keep-alive 75.
Redeploy the service. The recv() failed lines should stop.
Cause 2: the only pod was stopped or moved
With a single instance, every restart of that pod drops the connections it holds. This happens on each deployment, when the pod is out of memory, and when the cluster moves the pod to another node to consolidate nodes. Fix:- Run at least 2 instances: open the service Settings, then Resources, and set the minimum number of instances to 2 or more. See Instances and autoscaling.
- Configure a readiness probe so a new pod gets traffic only once it can answer. See Health checks.
- Handle
SIGTERMin your application: stop accepting new connections, finish the requests in flight, then exit. If your service needs more time to drain, raisedeployment.termination_grace_period_seconds. - Exit code 137 means the pod received
SIGKILL: either it ran out of memory (Reason: OOMKilled), or it did not stop within the termination grace period of step 3. See SIGKILL Signal 137.
Cause 3: the load balancer has no healthy target (AWS)
On AWS, traffic goes from the cloud load balancer to the NGINX ingress pods. If the load balancer sends traffic to nodes, a node without an NGINX pod fails the health check, and requests can hang or fail while NGINX pods move between nodes. With the AWS Load Balancer Controller, the load balancer sends traffic directly to the IP addresses of the NGINX pods, so node health no longer matters. It is controlled by the cluster advanced settingaws.eks.enable_alb_controller, which defaults to true. If an older cluster still has it set to false, enable it and redeploy the cluster.
Also keep at least 2 NGINX replicas with nginx.hpa.min_number_instances (default 2).
Check the service without NGINX
To find out whether the application or the path in front of it is at fault, send the same requests to the service directly withqovery port-forward. The traffic skips NGINX and the load balancer:
Related errors
client intended to send too large body: the request body is above the limit of 100 MB. Raisenetwork.ingress.proxy_body_size_mbon the service.- A
502or504after exactly 60 seconds is a timeout, not a reset. See NGINX timeout: increase the 60 second limit.