> ## Documentation Index
> Fetch the complete documentation index at: https://www.qovery.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 502 Bad Gateway: recv() failed (104: Connection reset by peer)

> Fix random 502 errors from the NGINX ingress when your service resets the connection: keep-alive timeouts, single instances, restarts and load balancer health checks.

Clients get a `502 Bad Gateway` from time to time, and the NGINX logs of the service show this line at the same moment:

```text theme={null}
[error] 164#164: *108859 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 203.0.113.10, server: api.example.com, request: "POST /data HTTP/1.1", upstream: "http://10.0.18.128:5000/data"
```

The same request usually works when it is sent again a few seconds later.

## 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 returns `502`.

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](https://www.qovery.com/docs/configuration/deployment/logs.md#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:

| Server | Default idle keep-alive timeout |
| - | - |
| Node.js `http.Server` (Express, NestJS, Fastify on Node) | 5 seconds (`keepAliveTimeout`) |
| Gunicorn | 2 seconds (`--keep-alive`) |
| Uvicorn | 5 seconds (`--timeout-keep-alive`) |

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`:

```js theme={null}
const server = app.listen(port);
server.keepAliveTimeout = 65000;
server.headersTimeout = 66000;
```

In NestJS, apply the same two values to `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:**

1. 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](https://www.qovery.com/docs/configuration/application.md#instances-%26-autoscaling).
2. Configure a readiness probe so a new pod gets traffic only once it can answer. See [Health checks](https://www.qovery.com/docs/configuration/service-health-checks.md).
3. Handle `SIGTERM` in your application: stop accepting new connections, finish the requests in flight, then exit. If your service needs more time to drain, raise [`deployment.termination_grace_period_seconds`](https://www.qovery.com/docs/configuration/service-advanced-settings#deployment-termination-grace-period-seconds).
4. 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](https://www.qovery.com/docs/getting-started/troubleshoot/overview.md#service-runtime-issues).

## 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 setting [`aws.eks.enable_alb_controller`](https://www.qovery.com/docs/configuration/cluster-advanced-settings#aws-eks-enable-alb-controller), which defaults to `true`. If an older cluster still has it set to `false`, enable it and redeploy the cluster.

> [!WARNING]
>
> Switching `aws.eks.enable_alb_controller` replaces the load balancer: public access to your services is cut for up to 10 minutes while the new load balancer and its DNS propagate. Plan a maintenance window.

Also keep at least 2 NGINX replicas with [`nginx.hpa.min_number_instances`](https://www.qovery.com/docs/configuration/cluster-advanced-settings#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 with [`qovery port-forward`](https://www.qovery.com/docs/cli/commands/port-forward). The traffic skips NGINX and the load balancer:

```bash theme={null}
qovery port-forward "<console-url-of-the-service>" --port 8080:<service-port>
curl -v http://localhost:8080/your/endpoint
```

If the errors also happen through the port-forward, the cause is in the application.

## Related errors

* `client intended to send too large body`: the request body is above the limit of 100 MB. Raise [`network.ingress.proxy_body_size_mb`](https://www.qovery.com/docs/configuration/service-advanced-settings#network-ingress-proxy-body-size-mb) on the service.
* A `502` or `504` after exactly 60 seconds is a timeout, not a reset. See [NGINX timeout: increase the 60 second limit](https://www.qovery.com/docs/getting-started/troubleshoot/nginx-timeout.md).

## Still stuck?

Contact Qovery support from the Console: click the help button at the bottom right, then **Contact Support**. You can also post in your dedicated Slack channel. Include the Console URL of the service, the deployment ID or the time of the error, and the error line from the logs. See [Help and Support](https://www.qovery.com/docs/getting-started/useful-resources/help-and-support.md).


## Related topics

- [NGINX timeout: increase the 60 second limit](https://www.qovery.com/docs/getting-started/troubleshoot/nginx-timeout.md)
- [Troubleshooting](https://www.qovery.com/docs/getting-started/troubleshoot/overview.md)
- [Setting Up Cloudflare and Custom Domain on Qovery](https://www.qovery.com/docs/getting-started/guides/advanced-tutorials/cloudflare-custom-domain.md)
- [Check ArgoCD connection](https://www.qovery.com/docs/api-reference/argocd/check-argocd-connection.md)
- [Cluster Advanced Settings](https://www.qovery.com/docs/configuration/cluster-advanced-settings.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.