Chapter 5 · Lesson 3 of 10
Fix it

Fix 504 Gateway Timeout behind a reverse proxy

A 504 Gateway Timeout means the reverse proxy reached for your app but got no answer in time, because the app is slow, hung, or not reachable from the proxy.

By the end you will

Tell a slow upstream from an unreachable one behind a reverse proxy, then fix the app, the timeout or the network path.

Time
20 min
Level
Intermediate
Check
4 questions
You need
  • A reverse proxy such as nginx or Caddy in front of an app
  • SSH access to the server

Your site shows 504 Gateway Timeout, or the proxy’s error page for it. The proxy is running and answered you, but it asked your app for a response and gave up waiting. The app may be slow, hung, or sitting on a network the proxy cannot reach. If the proxy got an outright refusal or a broken reply instead, that is a 502 and a different lesson: fix 502 Bad Gateway.

How it works#

A reverse proxy accepts the browser’s request and forwards it to an upstream, your app. It then waits. It has separate timers for connecting to the upstream and for reading the reply. If either runs out, it returns 504 to the browser. The HTTP status code is about time, not about content.

So there are two families of cause. The upstream is reachable but slow: a hung request, a slow query, an overloaded process. Or the upstream is not reachable at all, and packets disappear instead of being refused, which is what a firewall drop or a wrong IP looks like. A refusal is fast and gives 502. A silent drop makes the proxy wait until its timeout, which gives 504.

Step 1: Read the proxy error log#

The proxy knows which timeout fired. For nginx on a standard install:

bash
sudo tail -n 50 /var/log/nginx/error.log

Look for one of these:

  • upstream timed out (110: Connection timed out) while connecting to upstream: the proxy could not even open a connection. Think network path (Steps 3 and 4).
  • upstream timed out (110: Connection timed out) while reading response header from upstream: the connection worked and the app never answered. Think slow or hung app (Step 2).

The line also prints the upstream address, such as upstream: "http://127.0.0.1:3000/". Check that it is the address you meant.

Step 2: Time the app directly#

Bypass the proxy and talk to the app yourself, from the server:

bash
curl -s -o /dev/null -w 'status=%{http_code} total=%{time_total}s\n' http://127.0.0.1:3000/slow-path

If this also hangs or takes many seconds, the app is the problem, not the proxy. Then read the app’s own logs (see read container logs if it runs in Docker) and check CPU and memory with top or docker stats. Common causes are a database query that never returns, a call to an external service with no timeout, and a process stuck after running out of memory.

If the app answers fast here, move on: the app is fine and the proxy is not reaching it properly.

Step 3: Check that the proxy is pointed at the right place#

Open the proxy config and find the upstream:

bash
sudo nginx -T | grep -n -E 'proxy_pass|upstream|server_name'

Verify the host and port. Typical mistakes are a port that changed after a redeploy, localhost resolving to IPv6 ::1 while the app listens only on IPv4, and a container name used from a proxy that is not on the same Docker network. If the proxy runs in Docker, 127.0.0.1 means the proxy container itself, not the host or other containers. Use the service name on a shared network, as in Docker networks explained.

Step 4: Check the network and firewall#

When the proxy and the app are on different machines, or in different networks, test the connection from the proxy’s side:

bash
curl -v --max-time 5 http://198.51.100.20:3000/

A quick Connection refused means nothing listens there (back to 502 territory). A hang until the timeout means packets are dropped, which points to a firewall. Check the firewall on the app host (sudo ufw status) and your provider’s cloud firewall. See set up a firewall with ufw. Also check that the app binds to an address the proxy can reach. An app bound only to 127.0.0.1 cannot be reached from another machine.

Step 5: Adjust timeouts only when the work is legitimately slow#

If a route does real long work, such as an export, raising the read timeout for that route is reasonable:

nginx
location /export/ {
    proxy_pass http://127.0.0.1:3000;
    proxy_connect_timeout 5s;
    proxy_read_timeout 120s;
}

Keep proxy_connect_timeout short, since a slow connection almost always means a dead path. Raise proxy_read_timeout only on the routes that need it. Test the config and reload:

bash
sudo nginx -t && sudo systemctl reload nginx

If a CDN or load balancer sits in front of your proxy, it has its own timeout, and the smallest one in the chain wins.

Step 6: Fix the real cause#

A timeout setting hides a problem rather than solving it. For slow requests, add a timeout to outbound calls in the app, index the slow query, or move long jobs to a background worker and return a status URL. For an overloaded server, check memory and CPU, and add capacity or limits. For a hung process, fix the bug, and use a health check so a stuck container gets restarted.

Check it worked#

Repeat the request that failed:

bash
curl -s -o /dev/null -w 'status=%{http_code} total=%{time_total}s\n' https://app.example.com/slow-path

You should get the expected status with a sensible time. Then read the proxy error log again and confirm no new upstream timed out lines appear.

Prevent it#

  • Set timeouts in the app for every outbound call, shorter than the proxy’s.
  • Alert on response time, not only on up or down.
  • Keep proxy upstream addresses in one place, so a port change updates everywhere.

Edge cases and gotchas#

  • Several timeouts: The browser, a CDN, the proxy and the app each have limits. Find which one fired from the logs before changing any.
  • Streaming and uploads: Long uploads can hit client_body_timeout or size limits, which look different from an upstream timeout.
  • IPv6 localhost: localhost can resolve to ::1 while the app listens only on IPv4, so the connection hangs.
  • DNS caching in nginx: nginx resolves a hostname in proxy_pass at start, so a container that got a new IP can be unreachable until a reload.
  • Cold starts: A first request after idle may be slow because the app or database wakes up. That gives intermittent 504s.
  • Retries multiply waiting: With several upstreams, nginx can try the next one after a timeout, so the user waits for the sum.

Troubleshooting#

SymptomLikely causeFix
Log says timed out while connectingFirewall drop or wrong addressTest with curl from the proxy host, fix the rule or address
Log says timed out while reading response headerApp slow or hungTime it directly, read app logs, fix the cause
Works from the server, fails through the proxyWrong host, port or Docker networkFix proxy_pass, join the shared network
504 after exactly the same seconds each timeA timeout setting is firingFind which layer owns that value
504 only under loadApp out of workers or memoryAdd capacity, limit concurrency
504 after a redeployUpstream IP or port changedUpdate the config and reload

Quick reference#

bash
sudo tail -n 50 /var/log/nginx/error.log
curl -s -o /dev/null -w 'status=%{http_code} total=%{time_total}s\n' http://127.0.0.1:3000/
sudo nginx -T | grep -n proxy_pass
sudo nginx -t && sudo systemctl reload nginx

Next#

Continue with Fix a Docker container that keeps restarting.

Check what you learned4 questions
1What does a 504 tell you that a 502 does not?
2Which nginx directive sets how long the proxy waits for the upstream to send a response?
3curl to the app on 127.0.0.1:3000 from the server is fast, but the proxy still times out. What is most likely?
4What is a good first step when only one slow route returns 504?

Frequently asked questions

What causes a 504 Gateway Timeout in nginx?

nginx connected to or waited on the upstream and no response arrived within the timeout. The upstream may be slow, stuck, or unreachable because of a firewall or a wrong network or port.

What is the difference between 502 and 504?

A 502 means the proxy got a bad answer or no connection at all, such as a refused connection. A 504 means it waited and the upstream never replied in time.

Should I just increase proxy_read_timeout?

Only if the request is legitimately slow, such as a long report or upload. If the app is hung or unreachable, a longer timeout only makes users wait longer for the same error.

Why do I get 504 only on some pages?

Those routes probably do slow work, such as a heavy database query or a call to an external API that hangs. Test the app directly on that path and look at its logs.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios