502 vs 504: Bad Gateway vs Gateway Timeout
Both come from a proxy. 502 means the upstream gave a bad (or no) response; 504 means the upstream was too slow and the proxy stopped waiting.
502
HTTP 502 Bad Gateway
A proxy or gateway (Nginx, load balancer, CDN) got an invalid response — or no response — from the upstream server behind it.
Causes & fixes →
504
HTTP 504 Gateway Timeout
A proxy or gateway waited too long for the upstream server to respond and gave up.
Causes & fixes →| 502 | 504 | |
|---|---|---|
| Upstream state | Down, refusing connections or crashing | Up, but slow |
| Timing | Usually immediate | Exactly at the proxy timeout (e.g. 60s) |
| Typical log line | connect() failed (111: Connection refused) | upstream timed out (110) |
| Fix | Restart/fix upstream, correct address | Speed up the request or raise timeouts |
Rule of thumb
Fast failure = 502 (is it running?). Slow failure at a round number of seconds = 504 (why is it slow?).