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 →
502504
Upstream stateDown, refusing connections or crashingUp, but slow
TimingUsually immediateExactly at the proxy timeout (e.g. 60s)
Typical log lineconnect() failed (111: Connection refused)upstream timed out (110)
FixRestart/fix upstream, correct addressSpeed 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?).