Troubleshooting guide
504 Gateway Timeout: causes and fixes
A 504 means a proxy gave up waiting for the server behind it. Raising the timeout hides the symptom; finding the slow work fixes it.
4 min read9 sectionsUpdated
On this page
Key points
- Each layer has its own timeout: the shortest one in the chain decides when a 504 appears.
- The slow part is behind the proxy: a query, an external API or a hung worker.
- Traces show exactly where the time went for the requests that timed out.
- Long jobs belong in the background, not in a request.
| Layer | Typical default | Setting |
|---|---|---|
| CDN (varies by provider) | often around 100 s | Provider-specific origin timeout |
| Cloud load balancer | often 60 s idle | Idle timeout |
| Nginx | 60 s | proxy_read_timeout |
| App server | varies | Request or worker timeout (e.g. gunicorn --timeout 30) |
| Database client | often none | Statement or query timeout |
The first layer to give up returns the error. If gunicorn kills a worker at 30 s, Nginx sees the connection drop and returns 502; if Nginx gives up at 60 s first, you see 504. Knowing the chain explains why the same slow request can produce different codes.
Find the slow work
- Which requests? Look at your access logs or APM for the route and time of the 504s.
- Open a trace for one of them. The waterfall shows whether the time is in a database query, an external API call or your own code.
- Check dependencies: is the database slow for everyone, or only for this query? Is a third-party API timing out?
- Check saturation: if every worker is busy, new requests wait in a queue and time out without doing any work.
# Nginx: which upstream timed out, and for which request?
sudo grep -E 'upstream timed out' /var/log/nginx/error.log | tail -n 20| Cause | Fix |
|---|---|
| Slow SQL query | Add an index, remove N+1 patterns, paginate; see the slow query guide |
| External API slow | Set client timeouts and retries with backoff; cache results; fail fast |
| Long-running work (exports, reports) | Move to a background job; return 202 and poll or notify |
| Worker pool full | Find what holds workers; add capacity; use async I/O |
| Timeout chain inconsistent | Make inner timeouts shorter than outer ones, so the app can fail cleanly |
Catching it before users do
- Alert on p95 or p99 latency per route, not just on errors: timeouts are latency that crossed a line. See p95 latency explained.
- Watch database time per request in APM; slow queries usually creep up before they time out.
- Uptime checks with a response-time assertion, so a page that takes 25 seconds counts as failing.
In VitalRix APM, the endpoints view shows p50, p95 and p99 per route and flags routes whose p95 drifts well above their own 7-day baseline, and each slow request opens as a waterfall.
Should I just increase proxy_read_timeout?
Only if the work is legitimately long and cannot be moved to the background. Otherwise you trade a fast error for a long wait, and tie up workers longer.
Why do I get 504 only under load?
Under load, workers or connection pools fill up, so requests wait before any work starts. The fix is usually capacity or removing slow work from the request path.
Check it now, free
- Website down checker: Is it down for everyone or just me? Check any site now.
- Open port checker: Is a TCP port open, closed or filtered from the internet?
Related reading
- 408 Request Timeout: causes and how to fix it: Understand 408 Request Timeout: slow client uploads, idle keep-alive connections and proxy settings, and how it differs from a 504 Gateway Timeout.
- Nginx "upstream timed out (110)": how to fix it: Fix Nginx upstream timed out (110: Connection timed out) while reading response header: find the slow request, tune proxy timeouts and fix the backend.
- Cloudflare error 522: connection timed out: Fix Cloudflare error 522 "Connection timed out": the origin did not complete the TCP connection in time. Overloaded servers, dropped packets and wrong DNS.
- ERR_HTTP2_PROTOCOL_ERROR: causes and how to fix it: Fix ERR_HTTP2_PROTOCOL_ERROR in Chrome: invalid response headers, crashed backends behind HTTP/2 proxies, buffer limits and antivirus interference.
- 503 Service Unavailable: causes and fixes: Why servers return 503 Service Unavailable, from overload and maintenance to no healthy backends, how to diagnose each cause and how to recover and prevent it.
- Error: connect ETIMEDOUT: What ETIMEDOUT means, how it differs from ECONNREFUSED, the usual causes such as firewalls, security groups and wrong IPs, and how to set timeouts.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.