# 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.

URL: https://levelrail.com/learn/troubleshooting/fix-504-gateway-timeout/
Updated: 2026-11-27

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](https://levelrail.com/learn/deploy-operate/fix-502-bad-gateway/)).

## How it works

A [reverse proxy](https://levelrail.com/learn/glossary/#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](https://levelrail.com/learn/glossary/#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](https://levelrail.com/learn/deploy-operate/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](https://levelrail.com/learn/docker/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](https://levelrail.com/learn/server-basics/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](https://levelrail.com/learn/docker/docker-healthchecks/)) 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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Log says timed out while connecting | Firewall drop or wrong address | Test with curl from the proxy host, fix the rule or address |
| Log says timed out while reading response header | App slow or hung | Time it directly, read app logs, fix the cause |
| Works from the server, fails through the proxy | Wrong host, port or Docker network | Fix `proxy_pass`, join the shared network |
| 504 after exactly the same seconds each time | A timeout setting is firing | Find which layer owns that value |
| 504 only under load | App out of workers or memory | Add capacity, limit concurrency |
| 504 after a redeploy | Upstream IP or port changed | Update 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
```

## Related lessons

- [Fix 502 Bad Gateway](https://levelrail.com/learn/deploy-operate/fix-502-bad-gateway/)): the refused and invalid-reply side of the same proxy errors.
- [What is a reverse proxy](https://levelrail.com/learn/deploy-operate/what-is-a-reverse-proxy/)): how the proxy and upstream relate.
- [Docker networks explained](https://levelrail.com/learn/docker/docker-networks-explained/)): why 127.0.0.1 inside a container is not the host.
- [Set up a firewall with ufw](https://levelrail.com/learn/server-basics/set-up-a-firewall-with-ufw/)): check whether packets are being dropped.
- [Read container logs](https://levelrail.com/learn/deploy-operate/read-container-logs/)): see what the app says while the proxy waits.

## Next

Continue with [Fix a Docker container that keeps restarting](https://levelrail.com/learn/troubleshooting/fix-a-docker-container-that-keeps-restarting/)).

## 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

- [nginx docs, ngx_http_proxy_module](https://nginx.org/en/docs/http/ngx_http_proxy_module.html)
