DEV Community

upordown.online
upordown.online

Posted on Edited on

How to fix 504 Gateway Timeout: find whether it's nginx, the load balancer, Cloudflare, or a slow origin

Uptime monitoring from outside your own infrastructure that alerts you the minute your site goes down — upordown.online

A 504 Gateway Timeout is not an application error. It is a proxy error. Something in front of your app — nginx, an AWS load balancer, Cloudflare, a Kubernetes ingress — waited for an upstream response, hit a limit, and returned 504 to the visitor. The origin may still be working, slowly, or it may be stuck. Until you know which hop gave up, raising a random timeout will not help.

This is how to find the hop, read the timeout that fired, and fix the cause rather than hiding it.

Confirm it is a 504, and who sent it

From a machine that can reproduce the request:

curl -sS -D- -o /dev/null --max-time 120 https://example.com/slow-path
Enter fullscreen mode Exit fullscreen mode

Look at the status and the headers. A 504 from nginx often includes Server: nginx. Cloudflare adds cf-ray and usually an error code in the body (error code: 524 is Cloudflare's own "origin took too long", which is a cousin of 504). An AWS Application Load Balancer 504 typically has Server: awselb/2.0. The via and x-cache headers, if present, also tell you which layer answered.

If you cannot reproduce it yourself, get a HAR file or the cf-ray / request id from the person who can. A 504 that only happens for one URL or one customer is a different problem from a site-wide 504.

Find the timeout that actually fired

Every hop has its own limit, and they have to make sense together. The outermost timeout must be longer than the ones behind it, or the outer proxy will always lose the race.

Typical defaults:

  • nginx proxy_read_timeout — 60 seconds. Also check proxy_connect_timeout and proxy_send_timeout.
  • AWS ALB idle timeout — 60 seconds.
  • Cloudflare — 100 seconds on many plans, then a 524.
  • Gunicorn / uWSGI / Puma worker timeout — often 30 seconds.
  • Your language runtime — HTTP client timeouts to databases and APIs, sometimes infinite, sometimes 10 seconds.

On nginx, the error log is explicit:

upstream timed out (110: Connection timed out) while reading response header from upstream
Enter fullscreen mode Exit fullscreen mode

That line is a proxy_read_timeout. If you instead see a connect timeout, the origin never accepted the connection (it is down, saturated, or listening on the wrong socket). A read timeout means the connection was accepted and then nothing useful came back in time.

sudo grep -E "timed out|504" /var/log/nginx/error.log | tail
Enter fullscreen mode Exit fullscreen mode

For an ALB, the access logs have a target_processing_time that sits at or just above the idle timeout when this is the cause. In Kubernetes, check the ingress controller's timeout annotations; they are often left at 60 seconds while a report endpoint needs 90.

Is the origin slow, or is it stuck?

A timeout is a symptom. Raise it only if the work genuinely needs longer. Otherwise you will trade 504s for a pile of stuck workers.

On the origin, look at the same request id or the same URL around the timestamp:

# if you log request duration
sudo journalctl -u myapp --since "10 min ago" | grep -E "duration|timeout|504"
Enter fullscreen mode Exit fullscreen mode

Useful splits:

  • Always slow, same URL. The handler is expensive: a report, a large export, a missing index, an N+1 query. Fix the query or make the work asynchronous. Do not raise the public timeout to 5 minutes so a CSV export can finish in the request.
  • Suddenly slow, many URLs. A dependency is degraded: the database, a cache, a payment API, DNS inside the VPC. Check connection pool wait times and upstream latency. A 504 wave that starts at a deploy is often a lock, a migration, or a connection pool that is too small for the new code.
  • Connect timeouts, not read timeouts. The process is dead, not listening, or has too few workers to accept. That is not a timeout-tuning problem.
-- Postgres: queries running longer than your proxy timeout
SELECT pid, now() - query_start AS running, state, left(query, 120)
FROM pg_stat_activity
WHERE state != 'idle'
ORDER BY query_start;
Enter fullscreen mode Exit fullscreen mode

If you see queries older than the proxy timeout, the proxy already gave up and the database is still working for a client that is gone. That is how a single slow query turns into a pool of stuck backends and then a site-wide 504.

A 504 is what visitors see when a hop in front of you gives up; your origin logs may still look busy and healthy. upordown.online checks the public URL from outside, with a finite timeout, so a gateway timeout is an alert with a timestamp, not a screenshot in a support ticket.

What to change, and in what order

  1. Identify the hop that emitted the 504. Do not raise nginx if Cloudflare already cut the request off at 100 seconds.
  2. Align timeouts from the inside out. Origin worker timeout < ingress / nginx < load balancer < CDN. Each layer should wait a little longer than the one behind it, and the client (browser, mobile app, your monitor) should wait a little longer than the CDN.
  3. Fix the slow work if a page that used to take 400 ms now takes 70 seconds. Raising the timeout papers over it until the next traffic spike.
  4. Move long jobs off the request. Generate the export, then let the client poll or download it. A user-facing HTTP request should not run past a minute if you can help it.
  5. Load-test the new limit if you really must raise it. More in-flight requests means more memory, more database connections, more workers. A higher timeout without more capacity turns a few 504s into a complete stall.

For nginx, a targeted override is better than a global one:

location /admin/reports {
    proxy_read_timeout 120s;
    proxy_pass http://app;
}
Enter fullscreen mode Exit fullscreen mode

Leave / at 60 seconds. A slow admin report should not set the budget for the homepage.

Don't forget the monitor

If your uptime check hits a cheap URL with a 10-second timeout, it will stay green through a 504 on the slow path that actually matters. If it hits the slow path with a timeout shorter than the proxy, it will alert on a 504 that users with a longer client timeout might not even see. Point a check at the real path, and set its timeout just above the outermost proxy timeout, so it fails when visitors fail.

Related reading

Have you chased a 504 that wasn't the proxy you first blamed? Which hop was it actually, and how did you prove it?

Top comments (0)