Upstream vs edge
Nginx often sits in front of app servers or PHP-FPM. A 502 Bad Gateway usually means nginx could not get a valid HTTP response from upstream (connection refused, reset, or empty reply). A 504 Gateway Timeout means the upstream did not answer within proxy_read_timeout (or related timers). Read error_log at info or warn—the status line alone misleads clients.
Quick triage matrix
| Symptom | Check first | Typical fix |
|---|---|---|
| 502 | proxy_pass host/port, unix socket path, upstream health | Start backend; align socket path; fix firewall between tiers |
| 504 | Slow DB or cold JVM; CDN to origin path | Optimize app; raise timeouts only with caps; add caching |
| 413 | client_max_body_size in server/location | Match API gateway and app limits on every tier |
| SSL handshake | Chain file, SNI server_name order | Use fullchain.pem; separate default_server block |
bind() failed | ss -tulpn port collision | Stop duplicate master; fix systemd unit |
Logging that pays off
Add structured access_log fields for request_time, upstream_status, and upstream_response_time so postmortems do not depend on reproducing in a browser.
Related: nginx reverse proxy, nginx SSL, clear nginx cache, checking open ports.