nginx 504 Gateway Timeout: Slow Upstream or Wrong Timeout

504 means nginx waited for your app and gave up. Either the app really is too slow for the timeout, or the timeout is set for a faster app than you run.

What you'll see

Root causes

App slower than proxy_read_timeout

Default is 60s; anything slower gets cut. Report jobs, batch endpoints, and model inference are the usual offenders.

Upstream queue saturated

All app workers busy; new requests wait in the proxy until the timeout fires. Load-related, not request-related.

Deadlock or hang in the app

The request wedged on a lock, stuck DB query, or dead peer connection. Even huge timeouts won't help.

Fix it

  1. Find which requests time out and how long they take
    tail -f /var/log/nginx/access.log | grep ' 504 '
  2. Raise timeouts honestly for the slow route only
    location /reports/ {
      proxy_read_timeout 300s;
      proxy_send_timeout 60s;
    }
  3. If it's saturation, check upstream concurrency
    upstream app { server 127.0.0.1:8080 max_conns=50; }
  4. For anything minutes-long, move to a job queue instead
    # return 202 + job id; let a worker do the work; poll or push the result

Field note

Infinite timeouts are an outage waiting to happen: worker slots fill with stuck requests and healthy traffic dies too. Long work belongs in a queue; HTTP belongs in seconds.

Common questions

What's the default timeout before nginx gives up?

proxy_read_timeout defaults to 60 seconds. Failures clustering at exactly one minute are the fingerprint of this default being hit — raise it for the specific slow path, scoped in its own location block.

Should I just set a huge timeout globally?

No: global increases keep hung upstream connections (and their workers) alive far longer, hurting capacity under incidents. Scope raises to the endpoints that legitimately need them, and move genuinely long work to async jobs.

Ship it right the first time

Our most-documented failures, packaged as ready-to-ship starter kits: Docker, Kubernetes, and Terraform.

Browse the template store →

One-time. Yours to modify. Instant download from the NinjaOps template store.

Get new fixes by email

One short email when new fixes and production templates drop. No spam, unsubscribe anytime.

Partner pick — sponsored

Vultr — our performance pick for this stack

High-frequency cloud servers in 32 locations — deploy in 60 seconds, pay by the hour.

Get Vultr →
Also vetted

DigitalOcean — Predictable flat-bandwidth cloud — $200 credit for new accounts.

Get DigitalOcean →

We earn a commission if you buy through our links — it never costs you extra. More vetted tools on our picks hub · comparing clouds? DigitalOcean vs Vultr and vs AWS · full deals: DigitalOcean · Vultr · NordLayer · Semrush