---
title: "Fix nginx 504 Gateway Time-out and find which timeout fired"
description: "HTTP 504 Gateway Timeout means a gateway, such as nginx, stopped waiting for the server behind it. For an nginx 504, the error log names the step where it stopped, and each step has its own timeout."
canonical: "https://logbrew.co/en/errors/http-504"
last_updated: "2026-11-25"
---

# Fix nginx 504 Gateway Time-out and find which timeout fired

HTTP 504 Gateway Timeout means a gateway, such as nginx, stopped waiting for the server behind it. For an nginx 504, the error log names the step where it stopped, and each step has its own timeout.

nginx stopped waiting for your app, so read the `while …` part of the `upstream timed out` line and raise that timeout for the slow route only.

Tested on November 25, 2026 with nginx 1.31.8, curl 8.7.1, Python 3.13.5, FastAPI 0.165.0, uvicorn 0.54.0, and logbrew-fastapi 0.1.14

Canonical: https://logbrew.co/en/errors/http-504

## Which timeout sent the 504, and what the app did

| Timeout we set | Client got `504` | App answered `200` |
| --- | --- | --- |
| `proxy_read_timeout 3s`, the app needs 5 s | Yes, after 3.002 s | Yes, after 5 s |
| `proxy_connect_timeout 2s`, no answer | Yes, after 2.000 s | No |
| `proxy_send_timeout 2s`, the app reads nothing | Yes, after 2.048 s | No |
| 2 servers, `proxy_read_timeout 3s` | Yes, after 6.003 s | Yes, on both servers |
| `proxy_read_timeout 10s`, the app needs 5 s | No, `200` | Yes |
| The client gives up after 2 s | No, `499` | Yes |

6 requests through nginx 1.31.8 to a FastAPI app, sent by curl 8.7.1.

## Find the timeout that fired

### Check that nginx sent the 504

nginx answers with its own HTML page and its version in the last line. Our request waited 3.004 s, the `proxy_read_timeout` of that route. A 504 with another body came from another gateway, and its own logs name the timeout.

```bash
curl -sS -i 'http://127.0.0.1:28504/api/report?delay=5'
```

Output

```text
HTTP/1.1 504 Gateway Time-out
Server: nginx/1.31.8
Date: Wed, 25 Nov 2026 19:17:58 GMT
Content-Type: text/html
Content-Length: 167
Connection: keep-alive

<html>
<head><title>504 Gateway Time-out</title></head>
<body>
<center><h1>504 Gateway Time-out</h1></center>
<hr><center>nginx/1.31.8</center>
</body>
</html>
```

### Find upstream timed out in the error log

nginx writes 1 line at the default level `error` for each timeout. The end of the message names the step. We made each step time out in its own test.

Output

```text
2026/11/25 22:19:37 [error] 77764#0: *9 upstream timed out (60: Operation timed out) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /connect/report HTTP/1.1", upstream: "http://192.0.2.1:8080/report", host: "127.0.0.1:28504"
2026/11/25 22:19:39 [error] 77764#0: *11 upstream timed out (60: Operation timed out) while sending request to upstream, client: 127.0.0.1, server: , request: "POST /send/upload HTTP/1.1", upstream: "http://127.0.0.1:28507/upload", host: "127.0.0.1:28504"
2026/11/25 22:17:58 [error] 27219#0: *3 upstream timed out (60: Operation timed out) while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /api/report?delay=5 HTTP/1.1", upstream: "http://127.0.0.1:28505/api/report?delay=5", host: "127.0.0.1:28504"
```

### Log how long each side waited

Add these fields to a `log_format` in the `http` block, then run `nginx -s reload`. `request_time` is how long the client waited, and `upstream_response_time` is how long nginx waited for the upstream. `upstream_connect_time` is `-` when the connection never opened.

```bash
log_format timing '$remote_addr "$request" $status $body_bytes_sent '
                  'request_time=$request_time '
                  'upstream_connect_time=$upstream_connect_time '
                  'upstream_header_time=$upstream_header_time '
                  'upstream_response_time=$upstream_response_time '
                  'upstream_status=$upstream_status';
access_log /var/log/nginx/timing.log timing;
```

Output

```text
127.0.0.1 "GET /connect/report HTTP/1.1" 504 167 request_time=2.000 upstream_connect_time=- upstream_header_time=- upstream_response_time=2.001 upstream_status=504
127.0.0.1 "POST /send/upload HTTP/1.1" 504 192 request_time=2.048 upstream_connect_time=0.001 upstream_header_time=- upstream_response_time=2.004 upstream_status=504
127.0.0.1 "GET /api/report?delay=5 HTTP/1.1" 504 167 request_time=3.002 upstream_connect_time=0.001 upstream_header_time=- upstream_response_time=3.003 upstream_status=504
```

### Match the wait to a timeout

On each 504, `upstream_response_time` matched 1 timeout. It was 2.001 s for `proxy_connect_timeout 2s`, 2.004 s for `proxy_send_timeout 2s`, and 3.003 s for `proxy_read_timeout 3s`. All 3 directives have a default of `60s`. With the defaults, an upstream that needed 65 s got a 504 after 60.002 s.

Output

```text
127.0.0.1 "GET /default/report?delay=65 HTTP/1.1" 504 167 request_time=60.002 upstream_connect_time=0.000 upstream_header_time=- upstream_response_time=60.002 upstream_status=504
```

### Look for a second try

With 2 servers in an `upstream` block, nginx sent the request to the second server after the first timed out. The client waited 6.003 s for 1 504. `upstream_response_time` shows 2 times, and the error log has 2 `upstream timed out` lines.

Output

```text
127.0.0.1 "GET /pool/report?delay=5 HTTP/1.1" 504 167 request_time=6.003 upstream_connect_time=0.000, 0.001 upstream_header_time=-, - upstream_response_time=3.002, 3.001 upstream_status=504, 504
```

## Fix it

### Raise proxy_read_timeout for the slow route only

Set a larger `proxy_read_timeout` in the `location` of the slow route, not in the `http` block, then run `nginx -s reload`. At `10s`, the request that needed 5 s got its `200` after 5.017 s.

```bash
location /slow/ {
    proxy_pass http://127.0.0.1:28505/api/;
    proxy_read_timeout 10s;
}
```

Output

```text
127.0.0.1 "GET /slow/report?delay=5 HTTP/1.1" 200 11 request_time=5.017 upstream_connect_time=0.000 upstream_header_time=5.017 upstream_response_time=5.017 upstream_status=200
```

### Make the upstream answer sooner

A larger timeout only makes the client wait longer. Our app finished every request that reached it with `200`, also after nginx sent the 504. With the default `60s`, it answered 5 s after nginx gave up. Move slow work to a background job, answer `202` at once, and let the client ask for the result.

### Stop the second try on a timeout

In our 2-server test, both servers ran the slow request, and the client waited for both. If a second server will be as slow, remove `timeout` from `proxy_next_upstream`. With `proxy_next_upstream error;`, nginx tried 1 server and gave up after 3.001 s.

```bash
proxy_next_upstream error;
```

Output

```text
127.0.0.1 "GET /pool1/report?delay=5 HTTP/1.1" 504 167 request_time=3.001 upstream_connect_time=0.000 upstream_header_time=- upstream_response_time=3.001 upstream_status=504
```

### Fix a connect timeout at the upstream

`while connecting to upstream` means the upstream address sent nothing back. Our test address `192.0.2.1` never answers, so nginx gave up after 2 s with `upstream_connect_time` `-`. Check that the upstream runs and listens on the port in `proxy_pass`, and that no firewall drops the packets. The nginx docs say this timeout cannot usually be over 75 s.

```bash
nc -vz -w 3 192.0.2.1 8080
```

Output

```text
nc: connectx to 192.0.2.1 port 8080 (tcp) failed: Operation timed out
```

### Fix a send timeout at the upstream

`while sending request to upstream` means the upstream stopped reading the request body. Our upstream took the connection and read nothing. curl sent 33,554,432 bytes, and nginx gave up 2 s after its last write, not 2 s after the start. Check that the app reads the body at once and does not wait on other work first.

### Find the slow requests in LogBrew

LogBrew has no nginx integration. Its SDK in the app behind nginx records each request as a span with its duration. In our test, `init_logbrew` of logbrew-fastapi 0.1.14 sent 1 span per request. This command lists the requests that took longer than your `proxy_read_timeout`, here 3 s.

```bash
logbrew traces --project "<project_id>" --service report-api --min-duration-ms 3000 --since 1h
```

### Join the 504 to the app's trace

nginx passes the client's `traceparent` header on to the upstream, and logbrew-fastapi continues that trace. Log the header beside the status. The 504 line and the app's span then have the same trace ID, `4bf92f3577b34da6a3ce929d0e0e4736` in our test. Open it with `logbrew read trace`.

```bash
log_format trace '$remote_addr "$request" $status request_time=$request_time '
                 'traceparent=$http_traceparent';
access_log /var/log/nginx/trace.log trace;
```

Output

```text
127.0.0.1 "GET /api/report?delay=5 HTTP/1.1" 504 request_time=3.005 traceparent=00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
```

## What we saw when we tested it

We ran nginx 1.31.8 on macOS 27.2 at `127.0.0.1:28504`, in front of a FastAPI app on Python 3.13.5. The app waits the seconds in `?delay=` and then answers `200`, and logbrew-fastapi 0.1.14 records each request. curl 8.7.1 sent each request.

Each 504 came when `upstream_response_time` reached the timeout we set, or 2 timeouts with 2 servers. The app finished each request that reached it with `200`, also when the client got a 504 or gave up first.

|  | `$status` | `$request_time` | `$upstream_response_time` | `$upstream_status` |
| --- | --- | --- | --- | --- |
| `proxy_read_timeout 3s`, the app needs 5 s | `504` | 3.002 sec | 3.003 sec | `504` |
| `proxy_connect_timeout 2s`, no answer | `504` | 2 sec | 2.001 sec | `504` |
| `proxy_send_timeout 2s`, the app reads nothing | `504` | 2.048 sec | 2.004 sec | `504` |
| 2 servers, `proxy_read_timeout 3s` | `504` | 6.003 sec | `3.002, 3.001` | `504, 504` |
| `proxy_read_timeout 10s`, the app needs 5 s | `200` | 5.017 sec | 5.017 sec | `200` |
| The client gives up after 2 s | `499` | 2.006 sec | 2.006 sec | `-` |

## Why nginx has 3 timeouts

RFC 9110 section 15.6.5 says a 504 Gateway Timeout comes from a server that acts as a gateway or proxy and did not get a timely response from the upstream server it needed. So any API gateway can send it. nginx writes `Gateway Time-out` on its page.

The nginx docs of `ngx_http_proxy_module` give `proxy_connect_timeout`, `proxy_send_timeout`, and `proxy_read_timeout` a default of `60s`. The send and read timeouts count the time between 2 writes or 2 reads, not the whole transfer. An upstream that sends a little data in time never reaches them.

The `ngx_http_upstream_module` docs say `$upstream_response_time` keeps 1 time per server that nginx tried, with commas between them. That is why our 2-server test logged `3.002, 3.001`.

1. nginx connects to the upstream
2. nginx sends the request
3. nginx reads the response header

## What LogBrew records

logbrew-fastapi 0.1.14 sent 1 span for the request that got the 504 after 3.005 s. The span says `ok`, with `status_code` `200` and `duration_ms` `5005.964`, because the app finished after nginx gave up. We pointed the SDK at a local endpoint and kept the exact payload.

So LogBrew shows a slow request that worked, not a 504. The 504 is only in the nginx logs. Join them by the trace ID, or send the nginx lines as OTLP logs, as the HTTP 499 guide shows.

- `report-api`
  - Span: `GET /api/report`
  - `traceId`: `4bf92f3577b34da6a3ce929d0e0e4736`
  - `status`: `ok`
  - `status_code`: `200`
  - `duration_ms`: `5005.964`

## Questions people ask

### What does 504 Gateway Time-out mean in nginx?

nginx waited for the upstream server longer than its timeout and stopped. It sends its own HTML page with status 504 and writes `upstream timed out` to the error log.

### How do I fix a 504 Gateway Time-out in nginx?

Read the `while …` part of the `upstream timed out` line. For `reading response header`, raise `proxy_read_timeout` in the slow `location`, or make the upstream answer sooner.

### What is an API gateway timeout?

It is a 504 from the gateway in front of your API. The gateway waited longer than its timeout for the service behind it. In nginx, the error log line names which of 3 timeouts it was.

### Why does nginx send a 504 after 60 seconds?

`60s` is the default of `proxy_connect_timeout`, `proxy_send_timeout`, and `proxy_read_timeout`. Our upstream that needed 65 s got a 504 after 60.002 s.

### Does the app know that nginx sent a 504?

Not in our test. Our FastAPI app finished each request after nginx gave up, and its LogBrew span said `ok` with `status_code` `200`.

### What is the difference between 504 and 499?

With 504, nginx stopped waiting first and sent the client a 504. With 499, the client stopped waiting first, and nginx logged 499 with `upstream_status` `-`.

## See the slow request behind each 504

Add a LogBrew SDK to the app behind nginx, and find each slow request by its duration or trace ID from the dashboard, the CLI, or your AI agent. Start on the generous free tier, no card needed.

[Start Free](https://logbrew.co/en)

## Sources

- [RFC 9110, HTTP Semantics, 15.6.5 504 Gateway Timeout](https://www.rfc-editor.org/rfc/rfc9110#section-15.6.5)
- [nginx docs, Module ngx_http_proxy_module](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_read_timeout)
- [nginx docs, Module ngx_http_upstream_module, Embedded Variables](https://nginx.org/en/docs/http/ngx_http_upstream_module.html#var_upstream_response_time)
- [LogBrewCo/sdk, logbrew-fastapi](https://github.com/LogBrewCo/sdk/blob/main/python/logbrew_fastapi/README.md)

## Fix an error

- [Fix the script error that hides its message, file, and line](https://logbrew.co/en/errors/script-error)
- [Fix the Failed to fetch error and tell its 3 causes apart](https://logbrew.co/en/errors/failed-to-fetch)
- [Fix java.lang.NullPointerException in Kotlin and Android from its message](https://logbrew.co/en/errors/nullpointerexception)
- [Fix Minified React error #418 and #185 from the full message and your source line](https://logbrew.co/en/errors/minified-react-error)
- [Fix SyntaxError Cannot use import statement outside a module in the browser and in Node.js](https://logbrew.co/en/errors/cannot-use-import-statement-outside-a-module)
- [Fix AxiosError Network Error and find which of its 3 causes you have](https://logbrew.co/en/errors/axios-network-error)
- [Fix the HTTP 499 error and find out who stopped waiting](https://logbrew.co/en/errors/http-499)
- [Fix 413 Request Entity Too Large and find which layer refused it](https://logbrew.co/en/errors/http-413)
- [Show all PHP errors and catch the ones visitors never see](https://logbrew.co/en/errors/php)
- [JavaScript errors and JVM exceptions we reproduced, and how to fix each one](https://logbrew.co/en/errors)
