---
title: "修复 nginx 502 Bad Gateway，在 1 行日志中找到原因"
description: "HTTP 502 Bad Gateway 表示网关（例如 nginx）没有从它后面的服务器拿到有效响应。无论原因是什么，nginx 都返回同一个页面，原因写在它的错误日志里。"
canonical: "https://logbrew.co/zh-Hans/errors/http-502"
last_updated: "2026-11-27"
---

# 修复 nginx 502 Bad Gateway，在 1 行日志中找到原因

HTTP 502 Bad Gateway 表示网关（例如 nginx）没有从它后面的服务器拿到有效响应。无论原因是什么，nginx 都返回同一个页面，原因写在它的错误日志里。

nginx 没有从你的应用拿到可用的响应，所以请在 nginx 错误日志中找到这个请求。`Connection refused`、`prematurely closed connection` 或 `too big header` 会说明原因。

2026年11月27日 用 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-502

## 哪个原因返回了 502，LogBrew 又收到了什么

| 我们制造的原因 | 客户端收到 `502` | LogBrew 收到事件 |
| --- | --- | --- |
| 应用没有运行 | 是，0.001 秒后 | 否 |
| 应用在请求中途停止 | 是，1.008 秒后 | 否 |
| 6,165 字节的响应头，`proxy_buffer_size 4k` | 是 | 是，一个 `200` 的 span |
| 响应体中途抛出异常 | 否，一个被截断的 `200` | 是，一个 issue |
| 同样的响应头，`proxy_buffer_size 16k` | 否，`200` | 是，一个 `200` 的 span |
| 2 台服务器都停止 | 是 | 否 |

用 curl 8.7.1 经 nginx 1.31.8 向装有 logbrew-fastapi 0.1.14 的 FastAPI 应用发出的 6 个请求。

## 找出原因

### 确认 502 是 nginx 返回的

nginx 会返回自己的 157 字节 HTML 页面，最后一行写着它的版本。每种原因我们都收到了同一个页面，所以页面本身看不出原因。响应体不同的 502 来自另一个代理服务器，例如负载均衡器或 CDN，原因要看那个代理服务器的日志。

```bash
curl -sS -i 'http://127.0.0.1:28502/api/orders'
```

输出

```text
HTTP/1.1 502 Bad Gateway
Server: nginx/1.31.8
Date: Fri, 27 Nov 2026 06:44:56 GMT
Content-Type: text/html
Content-Length: 157
Connection: keep-alive

<html>
<head><title>502 Bad Gateway</title></head>
<body>
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.31.8</center>
</body>
</html>
```

### 在错误日志中找到 502

nginx 为每个 502 写 1 行 `error` 级别的日志。`while` 前面的词说明原因，后面的词说明步骤。在 Linux 上，第一行是 `connect() failed (111: Connection refused)`。我们的 Mac 把这个错误编号为 61，并在前面加上 `kevent() reported that`。

```bash
grep -E 'connect\(\) failed|response header from upstream' /var/log/nginx/error.log
```

输出

```text
2026/11/27 09:47:06 [error] 36998#0: *1 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /api/orders HTTP/1.1", upstream: "http://127.0.0.1:28503/api/orders", host: "127.0.0.1:28502"
2026/11/27 09:47:09 [error] 36998#0: *3 upstream sent too big header while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /api/session?bytes=6000 HTTP/1.1", upstream: "http://127.0.0.1:28503/api/session?bytes=6000", host: "127.0.0.1:28502"
2026/11/27 09:47:13 [error] 36998#0: *9 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /api/report HTTP/1.1", upstream: "http://127.0.0.1:28503/api/report", host: "127.0.0.1:28502"
2026/11/27 09:47:14 [error] 36998#0: *11 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /api/orders HTTP/1.1", upstream: "http://127.0.0.1:28503/api/orders", host: "127.0.0.1:28502"
```

### 记录是哪个上游做出的响应

把这些字段加到 `http` 块中的一个 `log_format` 里，然后运行 `nginx -s reload`。当 502 是 nginx 自己生成的时候，`upstream_status` 是 `502`。`upstream_response_time` 显示 nginx 等了多久。连接被拒绝时是 0.000 秒，应用在请求中途停止时是 1.007 秒。

```bash
log_format upstream '$remote_addr "$request" $status $body_bytes_sent '
                    'upstream_addr=$upstream_addr upstream_status=$upstream_status '
                    'upstream_response_time=$upstream_response_time '
                    'traceparent=$http_traceparent';
access_log /var/log/nginx/upstream.log upstream;
```

输出

```text
127.0.0.1 "GET /api/orders HTTP/1.1" 502 157 upstream_addr=127.0.0.1:28503 upstream_status=502 upstream_response_time=0.000 traceparent=-
127.0.0.1 "GET /api/session?bytes=6000 HTTP/1.1" 502 157 upstream_addr=127.0.0.1:28503 upstream_status=502 upstream_response_time=0.003 traceparent=00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01
127.0.0.1 "GET /api/report HTTP/1.1" 502 157 upstream_addr=127.0.0.1:28503 upstream_status=502 upstream_response_time=1.007 traceparent=00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01
```

### 查找 no live upstreams

我们在一个 `upstream` 块里放了 2 台服务器。停掉 1 台时，客户端从另一台拿到了 `200`，但错误日志里仍有一行 `connect() failed`。2 台都停掉时，nginx 写下 `no live upstreams`，没有重试就返回了 502。

输出

```text
2026/11/27 09:46:39 [error] 33103#0: *63 kevent() reported that connect() failed (61: Connection refused) while connecting to upstream, client: 127.0.0.1, server: , request: "GET /pool/orders HTTP/1.1", upstream: "http://127.0.0.1:28503/api/orders", host: "127.0.0.1:28502"
2026/11/27 09:46:39 [error] 33103#0: *63 no live upstreams while connecting to upstream, client: 127.0.0.1, server: , request: "GET /pool/orders HTTP/1.1", …
2026/11/27 09:46:39 [error] 33103#0: *65 no live upstreams while connecting to upstream, client: 127.0.0.1, server: , request: "GET /pool/orders HTTP/1.1", …
```

## 修复

### 启动应用，或修正 proxy_pass 中的端口

`Connection refused` 表示 `proxy_pass` 中的地址上没有进程在监听。在 nginx 所在主机上向这个地址发一个请求。如果 curl 也连不上，就启动应用或修正端口。如果应用是自己停掉的，先查明原因再重新启动。

```bash
curl -sS -i http://127.0.0.1:28503/api/orders
```

输出

```text
curl: (7) Failed to connect to 127.0.0.1 port 28503 after 0 ms: Couldn't connect to server
```

### 查明应用为什么关闭了连接

`prematurely closed connection while reading response header` 表示应用在发出状态行之前就关闭了连接。我们在一个耗时 5 秒的请求进行中用 `kill -9` 停掉了应用。nginx 在 1.007 秒后返回 502，应用没有写下任何日志，下一个请求得到 `Connection refused`。请在进程管理器和内核日志中查找停止的进程，例如因内存不足被杀掉的进程。

### 为大的响应头调大 proxy_buffer_size

我们的应用返回了 6,165 字节的响应头，而 `proxy_buffer_size` 是 `4k`。请同时调大 `proxy_buffers`。只设置 `proxy_buffer_size 16k` 时，`nginx -t` 报错 `"proxy_busy_buffers_size" must be less than the size of all "proxy_buffers" minus one buffer`。两者都设为 `16k` 后，同一个请求得到了 `200`。默认值是 1 个内存页，在我们的 Linux 容器中是 4k，在我们的 Mac 上是 16k，同样的响应头在 Mac 上可以通过。

```bash
location /fix/ {
    proxy_pass http://127.0.0.1:28503/api/;
    proxy_buffer_size 16k;
    proxy_buffers 8 16k;
}
```

输出

```text
127.0.0.1 "GET /fix/session?bytes=6000 HTTP/1.1" 200 11 upstream_addr=127.0.0.1:28503 upstream_status=200 upstream_response_time=0.001 traceparent=-
```

### 缩小响应头

在我们的测试中，1 个值为 6,000 字节的 Cookie 占了响应头的大部分。把会话数据放在服务器上，Cookie 中只发送它的 ID。也请去掉客户端用不到的响应头。

### 区分被截断的 200 和 502

`prematurely closed connection while reading upstream` 发生在响应头之后。nginx 已经向客户端发出了 `200`，所以只能关闭连接。我们的应用在导出 CSV 的中途抛出异常，curl 在 9,780 字节处停止。logbrew-fastapi 把这个异常作为 issue 发送了出去。

```bash
curl -sS -o /dev/null -w '%{http_code} %{size_download}\n' http://127.0.0.1:28502/api/export
```

输出

```text
curl: (18) transfer closed with outstanding read data remaining
200 9780
```

### 在 LogBrew 中查看应用看到了什么

LogBrew 没有 nginx 集成，所以收不到 502 页面，也收不到错误日志中的那一行。应用里的 SDK 发送的是应用看到的内容。这个命令列出在应用中失败的请求，例如我们的导出。对于过大的响应头，请像上面的日志格式那样记录 `traceparent`。这样 502 那一行和应用的 span 会有相同的链路 ID。用 `logbrew read trace` 打开它。

```bash
logbrew traces --project "<project_id>" --service orders-api --status error --since 1h
```

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

我们在 macOS 27.2 的 `127.0.0.1:28502` 上运行 nginx 1.31.8，放在一个基于 Python 3.13.5 和 uvicorn 0.54.0 的 FastAPI 应用前面。logbrew-fastapi 0.1.14 记录了每个请求，curl 8.7.1 发出了每个请求。我们设置了 `proxy_buffer_size 4k` 和 `proxy_buffers 8 4k`，这是内存页为 4k 的 Linux 上的默认值。

每个 502 的响应体都是同样的 157 字节。只有错误日志中的那一行说明了原因。响应体中途的异常没有产生 502，客户端收到的是 `200` 和更少的数据。

|  | `$status` | `$body_bytes_sent` | `$upstream_status` | `$upstream_response_time` |
| --- | --- | --- | --- | --- |
| 应用没有运行 | `502` | `157` | `502` | 0秒 |
| 应用在请求中途停止 | `502` | `157` | `502` | 1.007秒 |
| 6,165 字节的响应头，`proxy_buffer_size 4k` | `502` | `157` | `502` | 0.003秒 |
| 响应体中途抛出异常 | `200` | `11346` | `200` | 0.024秒 |
| 同样的响应头，`proxy_buffer_size 16k` | `200` | `11` | `200` | 0.001秒 |
| 2 台服务器都停止 | `502` | `157` | `502` | 0秒 |

## nginx 为什么返回 502

RFC 9110 第 15.6.3 节说，502 Bad Gateway 来自网关或代理服务器，它从后面的服务器收到了无效的响应。nginx 在完全没有收到响应时也会返回 502，例如连接被拒绝时。

nginx 的 `ngx_http_proxy_module` 文档说，`proxy_buffer_size` 存放响应的第一部分，也就是响应头。默认是 1 个内存页，文档给出的是 4k 或 8k，取决于平台。`proxy_buffers` 存放响应的其余部分。

nginx 只能在把响应头发给客户端之前返回 502。之后它只能关闭连接，我们那个被截断的 `200` 就是例子。

1. nginx 连接上游
2. nginx 把响应头读入 `proxy_buffer_size`
3. nginx 把响应发给客户端

## LogBrew 记录了什么

logbrew-fastapi 0.1.14 发送的是应用看到的内容。对于 6,165 字节的响应头，应用发送了 `status_code` 为 `200` 的 span，而客户端收到的是 502。对于导出，它把 `RuntimeError` 连同堆栈帧作为 issue 发送，span 的状态是 `error`。我们把 SDK 指向一个本地端点，并保留了原样的 payload。

连接被拒绝时，请求从未到达应用。对于在请求中途停止的应用，SDK 什么也没有发送。这些 502 只存在于 nginx 日志中。请像 HTTP 499 指南那样，把 nginx 的日志行作为 OTLP 日志发送。

- `orders-api`
  - Span: `GET /api/session`
  - `traceId`: `0af7651916cd43dd8448eb211c80319c`
  - `status`: `ok`
  - `status_code`: `200`
- `orders-api`
  - 问题: `GET /api/export failed`
  - `exception`: `RuntimeError`
  - 堆栈帧: `app.py:30 rows`

## 常见问题

### nginx 中的 502 Bad Gateway 是什么意思？

nginx 没有从后面的服务器拿到有效响应，所以返回了自己的 502 页面。错误日志中的那一行会说明原因（没有连接、连接被关闭，或者响应头超出了缓冲区）。

### 502 Bad Gateway 是什么？

它是网关或代理服务器在后面的服务器没有给出有效响应时返回的 HTTP 状态码（RFC 9110，第 15.6.3 节）。问题出在代理服务器和应用之间，而不是浏览器。

### 如何修复 nginx 中的 502 Bad Gateway？

在 nginx 错误日志中找到这个请求的 `[error]` 行。如果是 `Connection refused`，启动应用或修正端口。如果是 `prematurely closed connection`，查明应用为什么停止。如果是 `too big header`，调大 `proxy_buffer_size` 和 `proxy_buffers`。

### nginx 为什么提示 upstream sent too big header？

应用的响应头比 `proxy_buffer_size` 大。我们的响应头是 6,165 字节，而设置是 `4k`。请一起调大 `proxy_buffer_size` 和 `proxy_buffers`，或者发送更小的 Cookie。

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

502 是应用拒绝了连接、关闭了连接，或者发回了 nginx 无法使用的响应。504 是应用太慢，nginx 在超时后不再等待。HTTP 504 指南展示了每个超时。

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

在我们的测试中不知道。响应头过大时，应用的 LogBrew span 是 `ok`，`status_code` 为 `200`。连接被拒绝时，请求从未到达应用。

## 查看每个 502 背后应用做了什么

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

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

## 参考来源

- [RFC 9110, HTTP Semantics, 15.6.3 502 Bad Gateway](https://www.rfc-editor.org/rfc/rfc9110#section-15.6.3)
- [nginx docs, ngx_http_proxy_module, proxy_pass](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass)
- [nginx docs, ngx_http_proxy_module, proxy_buffer_size](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffer_size)
- [nginx docs, ngx_http_proxy_module, proxy_buffers](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_buffers)
- [nginx docs, ngx_http_upstream_module, max_fails](https://nginx.org/en/docs/http/ngx_http_upstream_module.html#max_fails)
- [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)
- [修复 nginx 的 504 Gateway Time-out，并找出是哪个超时到期了](https://logbrew.co/zh-Hans/errors/http-504)
- [我们复现过的错误，以及每个错误的修复方法](https://logbrew.co/zh-Hans/errors)
