HTTP Status Codes 502 vs HTTP Status Codes 504
The difference
Both come from a proxy in front of your app. 502 means the upstream answered badly or refused; 504 means it did not answer in time.
| 502 HTTP Status Codes · Bad Gateway | 504 HTTP Status Codes · Gateway Timeout | |
|---|---|---|
| Meaning | Invalid or no response from upstream (refused, reset) | Upstream did not respond before the proxy timeout |
| Who is wrong | Upstream process or network path | Upstream latency vs proxy timeout |
| Retry? | Idempotent requests: once, with backoff | Idempotent requests: once; the work may still be running |
| Typical trigger | App crashed, wrong port, keep-alive race | Slow query, long sync job, saturated workers |
| Fix | Restart/health-check upstream, keepAliveTimeout > LB idle | Speed up or queue the work, return 202, tune timeouts |
| Header to check | nginx error.log: connect() failed / upstream prematurely closed | nginx error.log: upstream timed out (110) |
Which one do I have?
- If it fails instantly → it is 502.
- If it fails after exactly the proxy timeout (29s, 60s, 100s) → it is 504.
- If Cloudflare shows 522 or 524 → the same split, at the CDN edge.