Skip to content

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.

How timeouts stack up

LayerTypical defaultSetting
CDN (varies by provider)often around 100 sProvider-specific origin timeout
Cloud load balanceroften 60 s idleIdle timeout
Nginx60 sproxy_read_timeout
App servervariesRequest or worker timeout (e.g. gunicorn --timeout 30)
Database clientoften noneStatement 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

  1. Which requests? Look at your access logs or APM for the route and time of the 504s.
  2. 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.
  3. Check dependencies: is the database slow for everyone, or only for this query? Is a third-party API timing out?
  4. Check saturation: if every worker is busy, new requests wait in a queue and time out without doing any work.
bash
# Nginx: which upstream timed out, and for which request?
sudo grep -E 'upstream timed out' /var/log/nginx/error.log | tail -n 20

Fix the cause, then tune timeouts

CauseFix
Slow SQL queryAdd an index, remove N+1 patterns, paginate; see the slow query guide
External API slowSet 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 fullFind what holds workers; add capacity; use async I/O
Timeout chain inconsistentMake 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.

Frequently asked questions

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

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.