Three Errors, Three Different Failures
502, 504, and 403 all sound like generic "something's wrong" errors, but each one tells you something specific about where the failure happened — and confusing them wastes time looking in the wrong place.
- 502 Bad Gateway — nginx tried to talk to your app and the connection itself failed or returned garbage.
- 504 Gateway Timeout — nginx connected fine, but your app took too long to respond.
- 403 Forbidden (from nginx itself) — nginx refused to even try, usually before your app was ever involved.
Here's what actually causes each one, with the fastest real fix.
502 Bad Gateway
502 Bad Gateway
nginx/1.25.3
[error] 12345#0: *1 connect() failed (111: Connection refused) while connecting
to upstream, client: 203.0.113.7, server: example.com,
upstream: "fastcgi://unix:/run/php/php8.3-fpm.sock:"
What it means: nginx made a connection attempt to the upstream (your app, PHP-FPM, or another proxy) and it failed outright — refused, unreachable, or returned a response nginx couldn't parse. This is a connection-level failure, not a slow-response failure.
The single most common real-world cause: PHP-FPM (or whatever process should be listening) isn't running, or its socket file is gone. Check first:
systemctl status php8.3-fpm
ls -la /run/php/php8.3-fpm.sock
If the socket file doesn't exist, PHP-FPM either crashed or was never started — start it and watch journalctl -u php8.3-fpm -f for why it died (a bad pool config or a fatal PHP error on startup are the usual reasons).
If the process is running but nginx still can't reach it, check that proxy_pass / fastcgi_pass actually matches where it's listening:
# Socket path or host:port must match exactly
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
# or
proxy_pass http://127.0.0.1:3000;
A mismatched port after a config change (app moved from 3000 to 3001, nginx config wasn't updated) is a close second on the list of real causes — always diff what the app is actually bound to (ss -tlnp) against what nginx is configured to hit.
504 Gateway Timeout
504 Gateway Time-out
nginx/1.25.3
[error] 12345#0: *1 upstream timed out (110: Connection timed out) while reading
response header from upstream, client: 203.0.113.7,
upstream: "http://127.0.0.1:3000/api/report"
What it means: unlike a 502, nginx did successfully connect to your upstream — it just didn't get a full response back within proxy_read_timeout (default 60 seconds). The connection succeeded; the response didn't finish in time.
The real fix is almost never just raising the timeout. A global proxy_read_timeout 600s; just means every stalled request now hangs for 10 minutes before failing instead of 1 — it hides the actual problem, which is usually one of:
- A slow database query or N+1 query pattern on that specific endpoint
- The upstream's worker/connection pool is exhausted, so requests queue instead of processing
- A downstream call the upstream itself makes (a third-party API, another internal service) is what's actually slow
If you've confirmed one specific endpoint is genuinely slow by design (a large report export, a bulk import), scope the timeout increase to just that location instead of the whole server:
location /api/report {
proxy_pass http://127.0.0.1:3000;
proxy_read_timeout 120s;
}
Check the upstream's own logs for how long it actually took to respond — if it's consistently near or past the timeout, that's your app's performance problem to fix, not nginx's config to paper over.