Short answer: 502 means the backend did not answer properly, 504 means it answered too late
Both are responses generated by a proxy such as nginx or a load balancer. The app did not send the 502; the proxy forwarded the request to the backend (the upstream) and something went wrong.
- 502 Bad Gateway — no valid response came back. The upstream is down, the port or socket path is wrong, the connection was refused or reset, or a worker died mid-request.
- 504 Gateway Timeout — the connection worked but no response arrived in time. The nginx
proxy_read_timeoutdefault is 60 seconds, and so is the AWS ALB idle timeout. "A 504 after exactly 60 seconds" is this case.
The nginx error log tells you which one
tail -n 50 /var/log/nginx/error.log
connect() failed (111: Connection refused) while connecting to upstream, upstream: "http://127.0.0.1:3000/"
connect() to unix:/run/php/php8.1-fpm.sock failed (2: No such file or directory) while connecting to upstream
connect() to unix:/run/php/php8.3-fpm.sock failed (13: Permission denied) while connecting to upstream
upstream prematurely closed connection while reading response header from upstream
upstream timed out (110: Connection timed out) while reading response header from upstream
- 111 Connection refused → nothing listens on that port (app down, port changed)
- 2 No such file or directory → the socket file is gone (path changed after a PHP upgrade)
- 13 Permission denied → the socket exists but the nginx user cannot open it
- prematurely closed → the worker died while answering (look for a crash or OOM in the app log)
- 110 timed out ... reading response header → a 504; the upstream is slow
The server: response header tells you who produced the error: nginx is nginx, awselb/2.0 is the ALB.
Step 1: call the upstream directly from the proxy host
curl -sv http://127.0.0.1:3000/healthz
ss -ltnp | grep 3000
curl -o /dev/null -s -w "%{http_code} %{time_total}s\n" http://127.0.0.1:3000/orders
If this fails, the problem is the app, not the proxy. Common cases: the app came up on another port, or it listens only on 127.0.0.1 while nginx runs in a different container. Inside an nginx container, 127.0.0.1 is nginx itself.
Step 2 (502): make the address and socket match
Upgrading PHP from 8.1 to 8.3 and removing the old version changes the socket path. Check that listen in the php-fpm pool config and fastcgi_pass in nginx point to the same path.
ls -l /run/php/
grep -E "^listen" /etc/php/8.3/fpm/pool.d/www.conf
# nginx
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
nginx -t && systemctl reload nginx
For Permission denied, set listen.owner and listen.group in the pool config to the user nginx runs as (www-data on Debian and Ubuntu). If 502s behind an ALB are only occasional, the app's keep-alive timeout is often shorter than the ALB idle timeout, so the app closes connections first. Make the app's value longer.
Step 3 (504): find why it is slow before touching timeouts
If time_total in step 1 goes past 60 seconds, the cause is inside the upstream. Typical ones: an index lost in a table migration, a slow query, waiting on an external API, or locks. Read the DB slow query log and the app log first. If the work is legitimately long (reports, uploads), raise the timeout for that path only.
location /reports/ {
proxy_pass http://app_backend;
proxy_read_timeout 180s; # for php-fpm: fastcgi_read_timeout
}
With an ALB in front, raise the ALB idle timeout too. Raise only one and the shorter one keeps cutting the request.
What not to do
- Raising timeouts to minutes without knowing the cause — the slow query stays, connections and workers are held longer, and everything slows down.
- Restarting only nginx on a 502 — the problem is usually on the upstream side.
The order to work in
- Read the errno and message in the nginx error log to tell the 502 and 504 causes apart
- Call the upstream directly with curl from the proxy host
- 502 → fix the upstream address, port, socket path or permissions
- 504 → find why it is slow (query, external call); adjust timeouts only for legitimately long work
To practise the 502 caused by a socket path change after a PHP upgrade, try the one-minute quiz 502 Bad Gateway: every page fails after a PHP upgrade.
Quick diagnostic checklist
- Use the
server:response header to see whether nginx or the ALB produced the error. - Find the upstream error message at the same time in
/var/log/nginx/error.log. - On
111: Connection refused, check that a process listens on the upstream port. - On
2: No such file or directory, compare the php-fpm socket path withfastcgi_pass. - On
13: Permission denied, checklisten.ownerandlisten.groupin the pool config. - Call the upstream directly with
curlfrom the proxy host and read the status andtime_total. - A 504 after exactly 60 seconds is the
proxy_read_timeoutor ALB idle timeout default. - Raise timeouts only for long-running paths, after checking slow queries and external calls.