DEBUG.WIKI
RackNerd;$21.99/年 · 1GB KVM
搬瓦工;先看线路,再选 VPS;CN2 GIA · 对应线路套餐;查看线路 →
腾讯云;让你的想法跑起来;轻量服务器 · 建站 / Docker / API;查看产品 →

Nginx 报 504 Gateway Time-out: upstream timed out (110: Connection timed out) 排查

排查 Nginx 反向代理触发 504 Gateway Time-out 报 upstream 110 连接超时的后端假死与超时参数对策。

故障现象

客户端访问 Web 接口耗时达到整整 60 秒后,界面弹出 504 Gateway Time-out。查看 Nginx 错误日志:

2026/11/15 17:38:00 [error] 14210#14210: *12045 upstream timed out (110: Connection timed out) while connecting to upstream, client: 203.0.113.10, server: api.example.com, request: "POST /order/pay HTTP/1.1", upstream: "http://10.244.1.80:8080/order/pay", host: "api.example.com"

Nginx 根本无法与上游后端 Pod 建立初始 TCP 连接。

核心机制与根因分析

与读取响应超时的 while reading response header 不同,此错误明确发生在 while connecting to upstream 阶段:

  1. TCP 握手无法完成:Nginx 发送 SYN 后,目标上游主机在超时时间内完全未回复 SYN-ACK。
  2. 诱因涵盖:
    • 目标 Pod 已经物理死亡或被调度走,但 Service 的 iptables 规则未及时刷新;
    • 后端容器所在节点的防火墙规则或云安全组单向阻断了网关所在网段;
    • 目标应用进程的全连接队列(accept backlog)彻底被慢查询打满,新连接被内核静默丢弃。

针对性解决方案

方案一:调小 proxy_connect_timeout 实现快速失败转移

避免单个假死后端拖累客户端等待长达 60 秒:

location / {
    proxy_pass http://backend_cluster;
    # 将连接握手超时严格限制在 3 到 5 秒
    proxy_connect_timeout 3s;
    proxy_read_timeout 60s;
    proxy_next_upstream error timeout http_502 http_504;
}

Nginx 在 3 秒内连不上会立即尝试下一个上游节点,保证客户端体验。