A 502 Bad Gateway from Nginx means Nginx received the request but couldn't get a valid response from the upstream application (Gunicorn, PHP-FPM, Node, etc.). Here's how to diagnose it.

1. Is the app actually running?

sudo systemctl status myapp
sudo systemctl status php8.2-fpm

The most common cause is simply that the upstream service crashed or isn't started. Restart it and check the logs.

2. Check the Nginx error log

sudo tail -f /var/log/nginx/error.log

It usually names the exact problem — "connection refused" (app down), "no such file" (wrong socket path), or "upstream timed out" (app too slow).

3. Verify the proxy target matches

Make sure proxy_pass (or the PHP-FPM socket path) points at the address/port the app is actually listening on. A mismatch after a config change is a frequent culprit.

4. Timeouts under load

If the app is slow, raise proxy_read_timeout or add more workers — but also investigate why responses are slow.

Quick checklist

App running? → correct socket/port? → permissions on the socket? → enough workers? Work through these and you'll clear almost every 502.