# 客户端在从上游读取响应头时关闭了连接

- **ID:** `nginx/client-closed-connection-while-reading-response`
- **领域:** nginx
- **类别:** network_error
- **验证级别:** ai_generated
- **修复率:** 82%

## 根因

客户端在Nginx完成从上游读取响应头之前提前断开连接，通常由浏览器超时或用户取消引起。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| nginx 1.18.0 | active | — | — |
| nginx 1.20.2 | active | — | — |
| nginx 1.22.1 | active | — | — |
| nginx 1.24.0 | active | — | — |
| nginx 1.25.3 | active | — | — |

## 解决方案

1. ```
   在location块中设置 proxy_ignore_client_abort on;
   ```
2. ```
   通过优化上游应用或启用缓存来减少响应生成时间：proxy_cache my_cache; proxy_cache_valid 200 1h;
   ```
3. ```
   增加 client_header_timeout 和 client_body_timeout 以防止过早断开：client_header_timeout 60s; client_body_timeout 60s;
   ```

## 无效尝试

- **** — The issue is client disconnection, not upstream timeout; high timeout may mask the problem but doesn't prevent it. (60% 失败率)
- **** — Keepalive settings affect connection reuse, not client disconnection during response reading. (80% 失败率)
- **** — Disabling buffering can increase memory usage and doesn't address the root cause of client disconnection. (70% 失败率)
