解决 Nginx 报错 upstream timed out 110 Connection timed out
分析 Nginx 反向代理向上游发起 TCP 握手超时报 110 Connection timed out 根因,提供防火墙、安全组与 proxy_connect_timeout 排查方案。
问题现象
用户访问 Nginx 代理的网站时页面等待数秒后返回 HTTP 504 Gateway Time-out。查看 Nginx 错误日志打印如下内容:
2026-11-15 11:00:00 [error] 12345#12345: *67890 upstream timed out (110: Connection timed out)
while connecting to upstream, client: 1.2.3.4, server: example.com,
request: "GET /api/v1/data HTTP/1.1", upstream: "http://10.0.0.50:8080/api/v1/data"
触发根因
注意错误日志中的关键字:while connecting to upstream,这与请求处理超时的 while reading response header 有本质区别:
- Nginx 向后端目标 IP:Port 发起 TCP 三次握手(SYN)完全未收到响应:数据包被静默丢弃(DROP)。
- 目标服务器内部防火墙(iptables/nftables/UFW)未放行端口。
- 云厂商 VPC 安全组(Security Group)未配置入方向放行规则。
- 后端应用虽然绑定了端口,但仅监听在
127.0.0.1本地回环地址,而非0.0.0.0。
诊断排查步骤
- 在 Nginx 所在服务器上手动测试目标上游端口连通性:
nc -zv -w 3 10.0.0.50 8080
如果直接报 Connection timed out,证明在网络路由或防火墙层被丢弃。
- 在后端服务器检查监听地址:
ss -tulpn | grep 8080
观察 Local Address 是否为 127.0.0.1:8080。
解决步骤与操作命令
步骤一:修正后端服务的监听绑定地址
若后端服务仅监听在本地,修改其启动配置(如 Spring Boot、Gunicorn、Node.js),将其绑定到 0.0.0.0:
server:
address: 0.0.0.0
port: 8080
重启服务后再次验证。
步骤二:放行后端服务器防火墙
在后端主机上开放端口(以 UFW 和 firewalld 为例):
# Ubuntu UFW
sudo ufw allow from <nginx-ip> to any port 8080 proto tcp
# CentOS Firewalld
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
sudo firewall-cmd --reload
步骤三:检查并配置云平台安全组规则
登录云控制台,为后端主机所在的 VPC 安全组增加入方向规则:协议 TCP,端口 8080,授权对象为 Nginx 服务器的内网 IP。
步骤四:在 Nginx 层面配置快速失败(避免客户端无限挂起)
编辑 nginx.conf,将连接超时调至合理范围:
proxy_connect_timeout 3s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
平滑重载 Nginx,网络通畅后访问恢复正常。


