宝塔面板反代在 BGP 多线主机上的 60 秒假死:`proxy_next_upstream` 默认不含 502,跨线回源全在干等
先看现场:BGP 多线入口,反代后端跨线
一台 BGP 多线主机跑宝塔面板,Nginx 做反向代理,后端挂三台源站:电信、联通、移动各一台。宝塔“负载均衡”插件默认轮询,用户从电信线路进来,请求可能被甩到联通源站,跨线 RTT 从 8ms 变成 120ms,丢包 2%。平时慢一点还能忍,直到某次联通源站 PHP-FPM 卡死,Nginx 返回 502,用户端直接看到错误页;如果是读超时,那就更经典——用户干等 60 秒,最后拿到 504。
这不是后端挂了,是宝塔生成的默认反代配置在跨线回源场景下根本没做故障切换。下面把默认配置和调优后的配置拉出来对比,直接给命令和代码。
宝塔默认配置到底写了什么
先别猜,用 nginx -T 把生效配置抓出来,重点看 upstream 和 location 里的 proxy 参数:
nginx -T | grep -E 'upstream|proxy_pass|proxy_next_upstream|proxy_connect_timeout|proxy_read_timeout|proxy_send_timeout'
宝塔负载均衡插件生成的 upstream 大致长这样,location 里只有最基本的转发:
upstream bt_load_balancing {
server 10.0.0.11:80 weight=1;
server 10.0.0.12:80 weight=1;
server 10.0.0.13:80 weight=1;
}
location / {
proxy_pass http://bt_load_balancing;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
没有 proxy_next_upstream,没有 max_fails,没有超时收敛。全靠 Nginx 默认值兜底,而默认值在跨线回源时就是灾难。
三个默认值,三个坑
proxy_next_upstream默认只有error timeout:后端返回 502、503、504 时,Nginx 认为“请求已经发出去了”,不会切下一台。用户直接吃 502。proxy_connect_timeout和proxy_read_timeout默认都是 60s:跨线丢包时,TCP 握手可能反复重传,读响应也可能卡住,用户就在那里等 60 秒。BGP 多线入口再快,也救不了回源跨线。- 非幂等请求默认不重试:哪怕你手动加了
http_502,POST 请求不加non_idempotent也不会切换。表单提交、API 写操作在故障后端上直接失败。
实测对比:默认 vs 调优
测试方法不玩虚的:后端网卡用 tc netem 模拟跨线延迟和丢包,用 iptables 丢包模拟读超时,用 Nginx 的 return 502 模拟后端错误。
tc qdisc add dev eth0 root netem delay 120ms loss 2%
iptables -A OUTPUT -p tcp --sport 80 -j DROP
# 后端 server 块里临时加 return 502 验证错误切换
- 宝塔默认:GET 遇到 502 立即失败,不重试;POST 遇到 502 立即失败;后端读超时,用户等满 60s 才拿到 504。
- 调优后:GET 502 在 3s 内切到下一台;POST 502 在 3s 内切(需
non_idempotent);读超时 12s 触发切换;fail_timeout10s 内把故障节点摘掉,后续请求不再打过去。
改法:把 upstream 和 location 补全
在宝塔站点配置文件里改,不要只改负载均衡插件生成的那一段。upstream 加健康检查参数,location 里把超时和重试补齐:
upstream bt_backend {
server 10.0.0.11:80 max_fails=2 fail_timeout=10s;
server 10.0.0.12:80 max_fails=2 fail_timeout=10s;
server 10.0.0.13:80 max_fails=2 fail_timeout=10s;
keepalive 32;
}
location / {
proxy_pass http://bt_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 3s;
proxy_send_timeout 15s;
proxy_read_timeout 12s;
proxy_next_upstream error timeout http_502 http_503 http_504 non_idempotent;
proxy_next_upstream_tries 2;
proxy_next_upstream_timeout 15s;
}
non_idempotent 需要 Nginx 1.9.13+,宝塔现在的版本都够。但 POST 重试有重复提交风险,只对幂等接口开,或者后端用唯一键兜底。别为了“高可用”把订单重复写两遍。
排错命令
改完先验证配置,再用 curl 打 502 看切换,最后用 ss 和 tcpdump 确认连接打到哪台后端:
nginx -T | grep -E 'proxy_next_upstream|proxy_connect_timeout|proxy_read_timeout|proxy_next_upstream_tries'
curl -o /dev/null -s -w 'code=%{http_code} time=%{time_total}\n' -X POST -d 'a=1' https://example.com/api
ss -tnp state established '( dport = :80 or sport = :80 )'
tcpdump -i eth0 -nn 'tcp port 80 and host 10.0.0.12' -c 20
日志格式补上 $upstream_response_time 和 $upstream_addr,否则你根本看不到切换发生在哪台后端、耗时多少:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'rt=$request_time urt=$upstream_response_time ua=$upstream_addr';
一个更根本的建议:反代后端别跨线
BGP 多线主机的价值是入口多线,不是让你把回源也撒到三条线上。轻云互联的 BGP 多线主机同机房内网互通,反代后端走内网回源,RTT 0.2ms,上面这些 60s、3s 的超时参数都不敏感。如果后端必须在异地,用轻云互联的 BGP 多线加内网专线,或者至少把 upstream 按线路分组,别让轮询把电信用户打到联通源站。
宝塔面板的负载均衡插件方便,但它生成的配置是“能跑”级别,不是“跨线故障切换”级别。改完记得 nginx -t,再 nginx -s reload,然后用 curl 连续打几次 502 验证,别等用户先发现。