Cloud & Operations · Practical News

502 Bad Gateway: Diagnose It in Four Layers

Source: Paddy Dance teamKeywords: 502 bad gateway troubleshooting

Layer one: is the upstream running

For PHP sites, check php-fpm processes and how it listens. Use ps to count workers and ss -lntp to confirm the socket or port exists. If the process is gone, 502 is the only possible outcome.

For Java or Node services, check that the application port is listening and read the tail of the startup log.

Layer two: does the configuration match

The fastcgi_pass or proxy_pass target must match what the upstream actually listens on. After a PHP version change or an fpm configuration edit, this is the most common mismatch.

Separate 502 from 504: connection refused means it cannot connect, upstream timed out means it connected but did not answer in time. The latter needs timeout tuning or slow-endpoint work.

Layer three: why did it die

The php-fpm slow and error logs, plus kernel OOM records (look for killed process in dmesg), are the three key pieces of evidence. Memory exhaustion killing workers is the most common hidden cause.

For intermittent 502s, focus on worker pool saturation and memory leaks rather than configuration errors.

FAQ

What is the difference between 502 and 504?

502 is an invalid response or failed connection to the upstream; 504 is a timeout after connecting. Check processes and ports first, then slow requests and timeouts.

Restarting php-fpm fixes it. Can I ignore the cause?

Restarting is relief, not a fix. Find out whether it was memory, saturation or a code loop, or it will recur.

Why do only some users get 502?

Usually one unhealthy backend among several, or a load balancer that has not removed the failing node. Verify each backend individually.

Related questions

502 vs 504 differencephp-fpm not runningnginx upstream error502 troubleshooting steps

Further reading

Sources & References

The following are public sources. Always check the latest requirements on the official authority or platform page.

Explain your case first, then confirm the scope

Tell us your entity, business model and current progress. We will confirm what we can assist with and what you need to do before discussing fees.