# 上游超时（110：连接超时）在连接上游时，客户端：10.0.0.1，服务器：example.com，上游："http://127.0.0.1:8080"

- **ID:** `nginx/upstream-timed-out-110-connection-timed-out-while-connecting-to-upstream`
- **领域:** nginx
- **类别:** timeout_error
- **错误码:** `110`
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

Nginx 无法与上游服务器建立 TCP 连接，因为服务器不可达、被防火墙阻止或过载，导致连接尝试超过 proxy_connect_timeout。

## 版本兼容性

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

## 解决方案

1. ```
   增加 proxy_connect_timeout 以允许更多连接时间：
proxy_connect_timeout 30s;
同时验证上游可达性：telnet 127.0.0.1 8080
检查防火墙规则：iptables -L -n | grep 8080
   ```
2. ```
   添加上游服务器健康检查并移除不健康的服务器：
upstream backend {
    server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
    server 10.0.0.2:8080 backup;
}
这允许 nginx 跳过无响应的服务器。
   ```
3. ```
   检查上游服务器是否正在运行并监听：
systemctl status myapp
netstat -tlnp | grep 8080
如有必要，重新启动上游服务。
   ```

## 无效尝试

- **** — The timeout occurs during the TCP handshake, not during data transfer; these settings do not affect connect timeout. (60% 失败率)
- **** — The error is at the transport layer; protocol version is irrelevant. (40% 失败率)
- **** — Keepalive only helps after a connection is established; it does not prevent connection timeouts. (30% 失败率)
