---
title: "修复 HTTP 499 错误，找出是谁先停止了等待"
description: "HTTP 499 是客户端在收到响应前关闭连接时 nginx 记录的状态码。客户端永远看不到它，而在我们的测试中，上游把每个请求都处理完了。"
canonical: "https://logbrew.co/zh-Hans/errors/http-499"
last_updated: "2026-11-21"
---

# 修复 HTTP 499 错误，找出是谁先停止了等待

HTTP 499 是客户端在收到响应前关闭连接时 nginx 记录的状态码。客户端永远看不到它，而在我们的测试中，上游把每个请求都处理完了。

2026年11月21日 用 nginx 1.31.8、curl 8.7.1、Python 3.13.5和logbrew CLI 0.1.96 实测

作者 [Furkan Erday](https://github.com/furkanerday). Furkan Erday 是 LogBrew 的创始人，每天用 Claude Code 和 Codex 开发 LogBrew。

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

## 谁先停止等待，nginx 记录了什么

| 谁先停止等待 | 客户端收到响应 | 记录为 `499` |
| --- | --- | --- |
| 没有，上游在 0.2 秒内响应 | `200` | `200` |
| 客户端，2 秒后 | 否 | `499` |
| 客户端，0.3 秒后 | 否 | `499` |
| nginx，在 `proxy_read_timeout 3s` 之后 | `504` | `504` |
| 客户端，nginx 继续等待 | 否 | `200` |

用 curl 8.7.1 经 nginx 1.31.8 发送的 5 个请求。最后 1 个请求开启了 `proxy_ignore_client_abort on`。

## 找出是谁先停止了等待

### 在访问日志中找到 499 行

nginx 只把 `499` 写进访问日志。在默认的 `combined` 格式里，这一行显示请求和发送的 0 字节，但看不出各方等了多久。

输出

```text
127.0.0.1 - - [21/Nov/2026:17:09:07 +0300] "GET /api/report?delay=5 HTTP/1.1" 499 0 "-" "curl/8.7.1"
127.0.0.1 - - [21/Nov/2026:17:09:08 +0300] "GET /api/report?delay=1 HTTP/1.1" 499 0 "-" "curl/8.7.1"
```

### 记录每一方等待的时间

在 `http` 块中，在默认日志旁边加一个 `log_format`，写入 `$request_time`、`$upstream_status` 和 `$upstream_response_time`。然后运行 `nginx -s reload`。

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

### 读取每条 499 行的 request_time

`request_time` 是客户端断开前等待的时间。我们的客户端分别在 2 秒和 0.3 秒后放弃，nginx 记录的是 2.006 和 0.301。`upstream_status` 为 `-`，因为上游还没有响应。

输出

```text
127.0.0.1 "GET /api/report?delay=5 HTTP/1.1" 499 0 request_time=2.006 upstream_status=- upstream_response_time=2.006
127.0.0.1 "GET /api/report?delay=1 HTTP/1.1" 499 0 request_time=0.301 upstream_status=- upstream_response_time=0.301
```

### 找出反复出现的同一个值

如果很多 499 行的 `request_time` 几乎相同，说明 nginx 前面有一个固定的超时，比如客户端自己的超时。3 个带 `--max-time 2` 的客户端产生了 3 行，时间在 2.008 到 2.011 秒之间。把这个值和同一路径 200 行的 `upstream_response_time` 比较。如果上游经常需要更久，那它对这个客户端来说就太慢了。

输出

```text
127.0.0.1 "GET /api/report?delay=4 HTTP/1.1" 499 0 request_time=2.011 upstream_status=- upstream_response_time=2.011
127.0.0.1 "GET /api/report?delay=4 HTTP/1.1" 499 0 request_time=2.011 upstream_status=- upstream_response_time=2.012
127.0.0.1 "GET /api/report?delay=4 HTTP/1.1" 499 0 request_time=2.008 upstream_status=- upstream_response_time=2.007
```

### 区分 499 和 504

如果 nginx 先停止等待，它会记录 `504`，客户端收到 504 页面。此时 `request_time` 就是你的 `proxy_read_timeout`，在我们的测试中是 3 秒，而 `upstream_status` 是 `504`，不是 `-`。错误日志里写的是 `upstream timed out`。

输出

```text
127.0.0.1 "GET /short/report?delay=5 HTTP/1.1" 504 167 request_time=3.001 upstream_status=504 upstream_response_time=3.001
2026/11/21 17:09:11 [error] 61151#0: *35 upstream timed out (60: Operation timed out) while reading response header from upstream, client: 127.0.0.1, server: , request: "GET /short/report?delay=5 HTTP/1.1", upstream: "http://127.0.0.1:28500/api/report?delay=5", host: "127.0.0.1:28499"
```

## 修复

### 让上游在客户端放弃前响应

如果 499 行的 `request_time` 都一样，而同一路径的 200 行耗时更长，就优化那个接口，或调大客户端的超时。像我们的 0.301 这样很短的 `request_time`，说明是客户端自己取消了请求，比如用户离开了页面。

### 停掉没人在等的工作

nginx 记录为 499 的两个请求，我们的上游都记录了 200。它在客户端离开 3 秒和 0.7 秒后才处理完。nginx 关闭了到上游的连接，但上游没有察觉。在耗时长的 handler 里检查连接是否已关闭，或把慢任务移到后台作业。

输出

```text
14:09:05.796 upstream got /api/report?delay=5
14:09:07.822 upstream got /api/report?delay=1
14:09:08.827 upstream answered 200 after 1.0s
14:09:10.802 upstream answered 200 after 5.0s
```

### 保持 proxy_ignore_client_abort 关闭

开启 `proxy_ignore_client_abort on` 后，虽然 curl 在 2 秒后已断开，nginx 仍等待上游，并记录了 `request_time` 为 5.006 的 `200`。499 从日志里消失了。默认值是 `off`。

```bash
proxy_ignore_client_abort off;
```

输出

```text
127.0.0.1 "GET /keep/report?delay=5 HTTP/1.1" 200 12 request_time=5.006 upstream_status=200 upstream_response_time=5.006
```

### 把 499 行发送到 LogBrew

LogBrew 没有 nginx 集成，但接收 OTLP 日志。这条命令把每条 499 和 504 行作为 1 条日志发送。把 `LOGBREW_INGEST_KEY` 设为你项目的一个 `server` 密钥。我们的本地 LogBrew API 返回了 `{}`，说明每一行都已接收。

```bash
grep -E '" (499|504) ' /var/log/nginx/timing.log |
  jq -Rn '{resourceLogs: [{resource: {attributes: [{key: "service.name", value: {stringValue: "nginx"}}]}, scopeLogs: [{logRecords: [inputs | {severityText: "WARN", body: {stringValue: .}}]}]}]}' |
  curl -sS https://api.logbrew.co/v1/logs -H "x-logbrew-key: $LOGBREW_INGEST_KEY" \
    -H 'Content-Type: application/json' --data-binary @-
```

输出

```text
{}
```

### 用 LogBrew CLI 搜索

每一行是 1 条级别为 `warning` 的日志，消息就是整行内容。搜索时在 499 两边加空格，这样路径或时间里的 499 就不会被匹配。

```bash
logbrew read logs --project "<project_id>" --service nginx --search ' 499 ' --since 1h
```

输出

```text
Logs (2)
- warning 127.0.0.1 "GET /api/report?delay=1 HTTP/1.1" 499 0 request_time=0.301 upstream_status=- upstream_response_time=0.301 service=nginx trace=3ded5398edda96df764625b26d6274f2 [unversioned / production]
- warning 127.0.0.1 "GET /api/report?delay=5 HTTP/1.1" 499 0 request_time=2.006 upstream_status=- upstream_response_time=2.006 service=nginx trace=4ad4df7e9d1f37c7dc182a77a1e83012 [unversioned / production]
```

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

我们在 macOS 27.2 上运行 nginx 1.31.8，监听 `127.0.0.1:28499`，后面是一个 Python 3.13 上游，它按 `?delay=` 中的秒数等待，然后返回 200。curl 8.7.1 发送了 5 个请求，其中 3 个带 `--max-time`。

两条 499 行的 `request_time` 都和 curl 超时相差不到 6 ms，`upstream_status` 为 `-`。504 行的值与 `proxy_read_timeout` 一致，`upstream_status` 为 `504`。

|  | `$status` | `$request_time` | `$upstream_status` |
| --- | --- | --- | --- |
| 没有人放弃 | `200` | 0.206秒 | `200` |
| 客户端 2 秒后放弃 | `499` | 2.006秒 | `-` |
| 客户端 0.3 秒后放弃 | `499` | 0.301秒 | `-` |
| nginx 3 秒后放弃 | `504` | 3.001秒 | `504` |
| 客户端放弃，nginx 继续等待 | `200` | 5.006秒 | `200` |

## 为什么只有 nginx 能看到 499

499 不是 HTTP 状态码。RFC 9110 定义了 4xx 状态码，499 不在其中。nginx 在 `src/http/ngx_http_request.h` 中把它定义为 `NGX_HTTP_CLIENT_CLOSED_REQUEST`，用于在 nginx 发送响应头之前就关闭连接的客户端。

客户端永远收不到 499，因为它的连接已经关闭。nginx 也会关闭到上游的连接，并把 `499` 写进访问日志。

在错误日志的默认级别 `error` 下，nginx 不会再写任何关于 499 的内容。在 `info` 级别下，它会写 `client prematurely closed connection`。504 在 `error` 级别就会出现。

1. 客户端关闭连接
2. nginx 关闭到上游的连接
3. nginx 把 499 写进访问日志

## LogBrew 记录了什么

LogBrew 把我们发送的每一行存为 `nginx` 服务的 1 条日志，级别为 `warning`。它把整行保存为消息，不拆分字段，所以请按文本搜索。

控制台、CLI 和 MCP 服务器读取这些日志的方式，和读取其他 LogBrew 日志一样。

- `nginx`
  - `level`: `warning`
  - 消息: `… 499 0 request_time=0.301 …`
  - `trace`: `3ded5398edda96df764625b26d6274f2`

## 常见问题

### HTTP 状态码 499 是什么意思？

客户端在 nginx 发送响应前关闭了连接，nginx 记录了 499。这个状态不属于 HTTP，客户端也永远收不到它。

### 499 错误是客户端导致的还是服务器导致的？

关闭连接的是客户端，但原因往往是上游太慢。把 499 行的 `request_time` 和同一路径 200 行的 `upstream_response_time` 进行比较。

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

499 表示客户端先停止等待。504 表示 nginx 在 `proxy_read_timeout` 之后先停止等待，客户端会收到 504。

### 如何修复 nginx 中的 499 错误？

从 `request_time` 中找到客户端的超时，然后让上游在此之前响应，或调大这个超时。`proxy_ignore_client_abort on` 只是把日志里的 499 藏起来。

### 上游知道客户端已经离开了吗？

在我们的测试中不知道。nginx 记录为 499 的两个请求，我们的 Python 上游都记录了 200，并在客户端离开后才处理完。

## 在 1 个地方搜索每条 499 行

把 nginx 日志行作为 OTLP 日志发送到 LogBrew，然后在控制台、CLI 或你的 AI 智能体中搜索。免费额度充足，无需绑卡。

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

## 参考来源

- [RFC 9110, HTTP Semantics, 15.5 Client Error 4xx](https://www.rfc-editor.org/rfc/rfc9110#section-15.5)
- [MDN, 504 Gateway Timeout](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/504)
- [LogBrew docs, Send OpenTelemetry data to LogBrew with OTLP](https://docs.logbrew.co/guides/opentelemetry)
- [LogBrewCo/docs, guides/opentelemetry.mdx](https://github.com/LogBrewCo/docs/blob/main/guides/opentelemetry.mdx)

## 更多错误修复

- [修复隐藏了消息、文件和行号的脚本错误](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)
- [我们复现过的错误，以及每个错误的修复方法](https://logbrew.co/zh-Hans/errors)
