HTTP
HTTP 504Gateway Timeout
HTTP 504 Gateway Timeout means a proxy or load balancer forwarded the request to a backend server, which accepted it but did not send a response before the proxy's timeout expired. The backend is reachable but slow; the fix is either to make the backend faster or, where the work is legitimately long, to raise the timeout or move the work out of the request.
What 504 means
Browser ──► Proxy ──► Application (connected, working… still working…)
│
timeout expires → 504 to browserThe proxy set a limit on how long it would wait for the upstream. The application was reachable (otherwise it would be a 502) but did not finish. The application may still be working on the request after the client has already seen an error, which is why retrying blindly can multiply the load.
Causes
| Cause | Where to look |
|---|---|
| Slow database query or lock contention | Slow-query log, SHOW PROCESSLIST, pg_stat_activity (3306, 5432) |
| External API call inside the request that hangs | Application logs, outbound timeouts |
| Long-running job executed synchronously (report, export, import) | Move to a background queue |
| Backend overloaded; requests queue behind each other | Worker metrics, CPU |
| DNS resolution inside the backend timing out | Resolver logs; see DNS |
| Network problem between proxy and backend (packet loss) | tracert, interface errors |
| Proxy timeout set lower than the application's own timeout | Proxy config |
Diagnosis for operators
- Correlate the 504 timestamps with the application log: what was that request doing?
- Reproduce directly against the backend, bypassing the proxy, with timing:
curl -o NUL -s -w "time_total: %{time_total}s\n" http://127.0.0.1:8080/slow-page- If the backend takes 90 seconds and the proxy allows 60, you have found it. Decide: optimise, or make asynchronous, or (last resort) raise
proxy_read_timeout/ProxyTimeout/ the load-balancer idle timeout for that path only. - Check the database for locks and long transactions.
Preventing it
- Set application-level timeouts on every outbound call.
- Run heavy work in background jobs and poll or notify for completion.
- Add caching for expensive reads.
- Alert on p95 latency before it reaches the proxy timeout.
Frequently asked questions
How do I fix a 504 as a visitor?
Wait a minute and reload. The server is overloaded or a request is stuck. If a specific action (a report, an export) always times out, the operator needs to make that action asynchronous.
What is Cloudflare error 524?
Cloudflare's version of a gateway timeout: it connected to your origin but the origin did not finish responding within 100 seconds. Same causes and fixes as 504.
Should I just raise the proxy timeout?
Only for requests that genuinely need long processing and cannot be made asynchronous. Raising timeouts globally hides slow queries and ties up proxy workers; fix the slow path instead.