解决 Nginx 报错 upstream timed out (110: Connection timed out) 的排查指南
排查与解决 Nginx 无法与后端上游建立 TCP 握手导致返回 504 并在日志提示 110 Connection timed out 的完整方案。
问题现象
客户端访问 Web 服务时等待较长时间,随后收到 HTTP 504 Gateway Timeout 响应。在 Nginx 错误日志中记录:
2026/11/15 15:30:10 [error] 4567#4567: *1001 upstream timed out (110: Connection timed out) while connecting to upstream, client: 192.168.1.5, server: example.com, request: "GET /api/v1/users HTTP/1.1", upstream: "http://10.0.0.55:8080/api/v1/users"
注意与 reading response header 不同,本处明确发生在 while connecting to upstream(TCP 握手建连阶段)。
触发根因
- Nginx 所在服务器与后端上游服务器(
10.0.0.55)之间的网络不通或被防火墙安全组(Security Group / iptables)静默丢包(DROP)。 - 后端服务器宕机,或者目标端口(8080)根本没有程序在监听,且防火墙未返回 RST 而是直接黑洞丢弃 SYN 报文。
- Nginx 与后端服务器跨 VPC 路由断开。
解决步骤与操作命令
步骤一:在 Nginx 服务器上测试与后端的四层网络连通性
使用 nc 或 curl 测试目标 IP 与端口:
nc -zvw 3 10.0.0.55 8080
# 或
curl -I --connect-timeout 5 http://10.0.0.55:8080
若命令卡住直至超时,证明在传输层发生阻塞丢包。
步骤二:检查后端服务器防火墙与监听状态
登录后端机器(10.0.0.55),确认服务是否在监听以及监听的网卡地址:
# 确认是否仅监听了 127.0.0.1 而非 0.0.0.0
ss -tulpn | grep :8080
若只监听了 127.0.0.1,外部 Nginx 无法访问,必须将应用绑定地址改为 0.0.0.0。
步骤三:放行云平台安全组与本地防火墙
在后端主机放行来自 Nginx 节点的 IP 流量:
sudo ufw allow from 10.0.0.0/24 to any port 8080 proto tcp
验证确认
再次从 Nginx 节点发起测试请求:
curl -I http://example.com/api/v1/users
响应立即返回 HTTP 200 OK,TCP 握手耗时从数秒下降至 1ms 以内,错误彻底排除。


