---
title: "修复 nginx 的 504 Gateway Time-out，并找出是哪个超时到期了"
description: "HTTP 504 Gateway Timeout 表示网关（例如 nginx）不再等待它后面的服务器。nginx 错误日志会写出它在哪一步停止等待，而每一步都有自己的超时。"
canonical: "https://logbrew.co/zh-Hans/errors/http-504"
last_updated: "2026-11-25"
---

# 修复 nginx 的 504 Gateway Time-out，并找出是哪个超时到期了

HTTP 504 Gateway Timeout 表示网关（例如 nginx）不再等待它后面的服务器。nginx 错误日志会写出它在哪一步停止等待，而每一步都有自己的超时。

nginx 不再等待你的应用，所以先看 `upstream timed out` 那一行里的 `while …` 部分，再只为这条慢路由调大对应的超时。

2026年11月25日 用 nginx 1.31.8、curl 8.7.1、Python 3.13.5、FastAPI 0.165.0、uvicorn 0.54.0和logbrew-fastapi 0.1.14 实测

Canonical: https://logbrew.co/zh-Hans/errors/http-504

## 是哪个超时返回了 504，应用又做了什么

| 我们设置的超时 | 客户端收到 `504` | 应用返回 `200` |
| --- | --- | --- |
| `proxy_read_timeout 3s`，应用需要 5 秒 | 是，3.002 秒后 | 是，5 秒后 |
| `proxy_connect_timeout 2s`，没有回应 | 是，2.000 秒后 | 否 |
| `proxy_send_timeout 2s`，应用什么都不读 | 是，2.048 秒后 | 否 |
| 2 台服务器，`proxy_read_timeout 3s` | 是，6.003 秒后 | 是，两台服务器都返回了 |
| `proxy_read_timeout 10s`，应用需要 5 秒 | 否，`200` | 是 |
| 客户端 2 秒后放弃 | 否，`499` | 是 |

curl 8.7.1 经 nginx 1.31.8 向 FastAPI 应用发送的 6 个请求。

## 找出到期的超时

### 确认 504 来自 nginx

nginx 会返回它自己的 HTML 页面，并在最后一行写出版本号。我们的请求等待了 3.004 秒，正是这条路由的 `proxy_read_timeout`。如果 504 的正文不一样，它来自另一个网关，要看那个网关自己的日志来找超时。

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

输出

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

### 在错误日志中找到 upstream timed out

每次超时，nginx 都会以默认级别 `error` 写 1 行。消息末尾写明是哪一步。我们在不同的测试里分别让每一步超时。

输出

```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"
```

### 记录每一方等待了多久

把这些字段加到 `http` 块中的 `log_format` 里，然后运行 `nginx -s reload`。`request_time` 是客户端等待的时间，`upstream_response_time` 是 nginx 等待上游的时间。连接从未建立时，`upstream_connect_time` 为 `-`。

```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;
```

输出

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

### 把等待时间对应到超时

每个 504 中，`upstream_response_time` 都对应 1 个超时。`proxy_connect_timeout 2s` 时是 2.001 秒，`proxy_send_timeout 2s` 时是 2.004 秒，`proxy_read_timeout 3s` 时是 3.003 秒。这 3 个指令的默认值都是 `60s`。使用默认值时，一个需要 65 秒的上游在 60.002 秒后收到 504。

输出

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

### 检查是否有第二次尝试

`upstream` 块里有 2 台服务器时，第一台超时后 nginx 把请求发给了第二台。客户端为 1 个 504 等了 6.003 秒。`upstream_response_time` 显示 2 个时间，错误日志里有 2 行 `upstream timed out`。

输出

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

## 修复

### 只为慢路由调大 proxy_read_timeout

在慢路由的 `location` 中设置更大的 `proxy_read_timeout`，不要放在 `http` 块中，然后运行 `nginx -s reload`。设为 `10s` 后，需要 5 秒的请求在 5.017 秒后收到了 `200`。

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

输出

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

### 让上游更快响应

调大超时只会让客户端等得更久。我们的应用以 `200` 完成了每个到达它的请求，即使 nginx 已经发出了 504。使用默认的 `60s` 时，应用在 nginx 放弃 5 秒后才响应。把慢任务移到后台作业，立即返回 `202`，让客户端稍后再来取结果。

### 超时后不再尝试第二台

在我们的 2 台服务器测试中，两台服务器都执行了这个慢请求，客户端也等了两次。如果第二台服务器同样会慢，就从 `proxy_next_upstream` 中去掉 `timeout`。设为 `proxy_next_upstream error;` 后，nginx 只尝试了 1 台服务器，3.001 秒后放弃。

```bash
proxy_next_upstream error;
```

输出

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

### 在上游修复连接超时

`while connecting to upstream` 表示上游地址没有任何回应。我们的测试地址 `192.0.2.1` 永远不会回应，所以 nginx 在 2 秒后放弃，`upstream_connect_time` 为 `-`。检查上游是否在运行、是否在 `proxy_pass` 中的端口上监听，以及是否有防火墙丢弃了数据包。nginx 文档说明这个超时通常不能超过 75 秒。

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

输出

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

### 在上游修复发送超时

`while sending request to upstream` 表示上游不再读取请求体。我们的上游接受了连接，但什么都没读。curl 发送了 33,554,432 字节，nginx 在最后一次写入后 2 秒放弃，而不是开始后 2 秒。检查应用是否立即读取请求体，而不是先等待其他工作。

### 在 LogBrew 中找到慢请求

LogBrew 没有 nginx 集成。它的 SDK 装在 nginx 后面的应用里，会把每个请求连同耗时记录为一个 Span。在我们的测试中，logbrew-fastapi 0.1.14 的 `init_logbrew` 为每个请求发送了 1 个 Span。这条命令会列出耗时超过你的 `proxy_read_timeout`（这里是 3 秒）的请求。

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

### 把 504 关联到应用的链路

nginx 会把客户端的 `traceparent` 请求头转发给上游，logbrew-fastapi 会延续这条链路。把这个请求头和状态码一起记录到日志里。这样 504 那一行和应用的 Span 就有相同的链路 ID，在我们的测试中是 `4bf92f3577b34da6a3ce929d0e0e4736`。用 `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;
```

输出

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

## 我们测试时看到的结果

我们在 macOS 27.2 上运行 nginx 1.31.8，监听 `127.0.0.1:28504`，前面挡着一个运行在 Python 3.13.5 上的 FastAPI 应用。应用会等待 `?delay=` 中的秒数，然后返回 `200`，logbrew-fastapi 0.1.14 会记录每个请求。每个请求都由 curl 8.7.1 发送。

每个 504 都出现在 `upstream_response_time` 达到我们设置的超时之时，2 台服务器时则是 2 个超时。应用以 `200` 完成了每个到达它的请求，即使客户端收到了 504 或提前放弃。

|  | `$status` | `$request_time` | `$upstream_response_time` | `$upstream_status` |
| --- | --- | --- | --- | --- |
| `proxy_read_timeout 3s`，应用需要 5 秒 | `504` | 3.002秒 | 3.003秒 | `504` |
| `proxy_connect_timeout 2s`，没有回应 | `504` | 2秒 | 2.001秒 | `504` |
| `proxy_send_timeout 2s`，应用什么都不读 | `504` | 2.048秒 | 2.004秒 | `504` |
| 2 台服务器，`proxy_read_timeout 3s` | `504` | 6.003秒 | `3.002, 3.001` | `504, 504` |
| `proxy_read_timeout 10s`，应用需要 5 秒 | `200` | 5.017秒 | 5.017秒 | `200` |
| 客户端 2 秒后放弃 | `499` | 2.006秒 | 2.006秒 | `-` |

## 为什么 nginx 有 3 个超时

根据 RFC 9110 第 15.6.5 节，504 Gateway Timeout 由网关或代理服务器返回，表示它没有及时从所需的上游服务器收到响应。所以任何 API 网关都可能返回它。nginx 在页面上写的是 `Gateway Time-out`。

`ngx_http_proxy_module` 文档给 `proxy_connect_timeout`、`proxy_send_timeout` 和 `proxy_read_timeout` 的默认值都是 `60s`。发送和读取超时计算的是 2 次写入或 2 次读取之间的时间，而不是整个传输。按时发送少量数据的上游永远不会触发它们。

`ngx_http_upstream_module` 文档说明，`$upstream_response_time` 会为 nginx 尝试过的每台服务器保存 1 个时间，并用逗号分隔。所以我们的 2 台服务器测试记录的是 `3.002, 3.001`。

1. nginx 连接上游
2. nginx 发送请求
3. nginx 读取响应头

## LogBrew 记录了什么

对于 3.005 秒后收到 504 的那个请求，logbrew-fastapi 0.1.14 发送了 1 个 Span。这个 Span 显示 `ok`，`status_code` 为 `200`，`duration_ms` 为 `5005.964`，因为应用在 nginx 放弃之后才完成。我们把 SDK 指向一个本地端点，原样保存了它发送的内容。

所以 LogBrew 显示的是一个成功但很慢的请求，而不是 504。504 只存在于 nginx 日志中。可以通过链路 ID 把两者关联起来，或者像 HTTP 499 指南那样把 nginx 日志行作为 OTLP 日志发送。

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

## 常见问题

### nginx 中的 504 Gateway Time-out 是什么意思？

nginx 等待上游服务器的时间超过了超时，于是停止等待。它会返回自己的 HTML 页面，状态码为 504，并在错误日志中写入 `upstream timed out`。

### 如何修复 nginx 中的 504 Gateway Time-out？

先看 `upstream timed out` 那一行的 `while …` 部分。如果是 `reading response header`，就在慢的 `location` 中调大 `proxy_read_timeout`，或者让上游更快响应。

### 什么是 API 网关超时？

它是你的 API 前面的网关返回的 504。网关等待后面的服务超过了超时。在 nginx 中，错误日志那一行会写明是 3 个超时中的哪一个。

### 为什么 nginx 在 60 秒后返回 504？

`60s` 是 `proxy_connect_timeout`、`proxy_send_timeout` 和 `proxy_read_timeout` 的默认值。我们需要 65 秒的上游在 60.002 秒后收到了 504。

### 应用知道 nginx 返回了 504 吗？

在我们的测试中不知道。我们的 FastAPI 应用在 nginx 放弃后才完成每个请求，它在 LogBrew 中的 Span 显示 `ok`，`status_code` 为 `200`。

### 504 和 499 有什么区别？

504 是 nginx 先停止等待，并向客户端返回 504。499 是客户端先停止等待，nginx 记录 499，且 `upstream_status` 为 `-`。

## 查看每个 504 背后的慢请求

在 nginx 后面的应用中加入 LogBrew SDK，就能在控制台、CLI 或你的 AI 智能体中按耗时或链路 ID 找到每个慢请求。免费额度充足，无需绑卡。

[免费开始](https://logbrew.co/zh-Hans)

## 参考来源

- [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)

## 修复错误

- [修复隐藏了消息、文件和行号的脚本错误](https://logbrew.co/zh-Hans/errors/script-error)
- [修复 Failed to fetch 错误，分清它的 3 种原因](https://logbrew.co/zh-Hans/errors/failed-to-fetch)
- [从消息入手修复 Kotlin 和 Android 中的 java.lang.NullPointerException](https://logbrew.co/zh-Hans/errors/nullpointerexception)
- [从完整消息和你自己的代码行入手修复 Minified React error #418 和 #185](https://logbrew.co/zh-Hans/errors/minified-react-error)
- [在浏览器和 Node.js 中修复 Cannot use import statement outside a module](https://logbrew.co/zh-Hans/errors/cannot-use-import-statement-outside-a-module)
- [修复 AxiosError Network Error，找出你遇到的是 3 种原因中的哪一种](https://logbrew.co/zh-Hans/errors/axios-network-error)
- [修复 HTTP 499 错误，找出是谁先停止了等待](https://logbrew.co/zh-Hans/errors/http-499)
- [修复 413 Request Entity Too Large，找出是哪一层拒绝了请求体](https://logbrew.co/zh-Hans/errors/http-413)
- [显示所有 PHP 错误并抓住访客从未看到的错误](https://logbrew.co/zh-Hans/errors/php)
- [我们复现过的错误，以及每个错误的修复方法](https://logbrew.co/zh-Hans/errors)
