# 502 Bad Gateway (Ingress/Nginx) — upstream prematurely closed connection

- **ID:** `kubernetes/ingress-nginx-502-bad-gateway-upstream`
- **Domain:** kubernetes
- **Category:** network_error
- **Error Code:** `502`
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

The backend service (upstream) is not responding or closes connections prematurely, often due to high load or misconfigured timeouts.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| ingress-nginx 1.7.0 | active | — | — |
| Kubernetes 1.27 | active | — | — |
| nginx 1.23 | active | — | — |

## Workarounds

1. **Check upstream service health: `kubectl get pods -l app=<service>` and `kubectl logs <pod>`. Increase resources or add horizontal pod autoscaling if under load.** (85% success)
   ```
   Check upstream service health: `kubectl get pods -l app=<service>` and `kubectl logs <pod>`. Increase resources or add horizontal pod autoscaling if under load.
   ```
2. **Add ingress annotation to adjust timeouts: `nginx.ingress.kubernetes.io/proxy-read-timeout: '600'` and `nginx.ingress.kubernetes.io/proxy-send-timeout: '600'`.** (80% success)
   ```
   Add ingress annotation to adjust timeouts: `nginx.ingress.kubernetes.io/proxy-read-timeout: '600'` and `nginx.ingress.kubernetes.io/proxy-send-timeout: '600'`.
   ```

## Dead Ends

- **** — Only temporarily clears the issue; the upstream problem remains. (60% fail)
- **** — Does not address the upstream service's inability to handle connections. (70% fail)
- **** — May mask the issue but can cause resource exhaustion if upstream is truly broken. (50% fail)
