0%

Cloudflare + AWS NLB + Nginx 偶发 520 / 522 / 525 / 499 排查记录(进行中)

  • Argo
  • Nginx keepalive
  • Linux TCP bbr

1. 背景与架构

  • 背景:服务从阿里云迁移至 AWS us-east-2(俄亥俄) 后,部分客户端(主要位于新加坡 SG)访问域名时偶发报错。
  • 访问链路:
    客户端 -> Cloudflare -> AWS NLB(单可用区) -> EC2 (Nginx) -> 后端应用 (<app_port>)
  • 源站证书:使用 Cloudflare 签发的 15 年 Origin CA 证书,配置在 Nginx 上。
  • 异常状态码:Cloudflare 日志中出现 525、522、520、524、499,且 originResponseStatus 均为 0。

2. 排查过程

第一阶段:调整代理超时并开启耗时日志(排除应用层卡死)

最初怀疑后端 Java 接口超时导致异常(CF 免费版回源等待上限为 100s,Nginx 默认等待后端超时为 60s),因此将 Nginx proxy_read_timeout 设为 80s,并新增 api_timing 日志监控接口耗时:

1
2
3
4
5
6
log_format api_timing '$remote_addr [$time_local] "$request" '
'status=$status upstream_status=$upstream_status '
'upstream_addr=$upstream_addr '
'request_time=$request_time '
'upstream_time=$upstream_response_time '
'connect_time=$upstream_connect_time';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
location /api {
proxy_pass http://my_app/api;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_connect_timeout 30s;
proxy_read_timeout 80s;
proxy_send_timeout 120s;
client_max_body_size 30M;

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 X-Forwarded-Proto https;
}

排查结果:

  • 对比 CF 报错时间点前后 api_timing.log:所有请求均返回 status=200 upstream_status=200,connect_time=0.000,P95 耗时 0.333s(最大仅 2.685s)。
  • 且因 proxy_read_timeout 为 80s,若后端卡死应先由 Nginx 报 504,而非等 CF 100s 报 524。基本排除 Java 应用与 Nginx 上游代理问题。

第二阶段:调大长连接超时 + 开启 info 级别错误日志

怀疑默认 keepalive_timeout 65 过小导致与 Cloudflare 回源连接池复用冲突,同时原 error_log 为 error 级别会忽略连接/握手异常,于是调整为:

1
2
3
4
keepalive_timeout 180;
error_log /var/log/nginx/gateway_error.log info;
access_log /var/log/nginx/api_access.log;
access_log /var/log/nginx/api_timing.log api_timing;

排查结果:

  • 调整后 520 / 524 消失,但夜间(北京时间 21:00 ~ 02:00)仍偶发 522 与 525,且对应时间点 Java 应用依旧无请求进入记录。
  • 检索 gateway_error.log(info 级别)终于捕获到明确现场:
1
2
3
4
5
6
2026/11/19 18:18:25 [info] ... client timed out (110: Connection timed out) while SSL handshaking, client: 172.68.xxx.xxx, server: 0.0.0.0:443
2026/11/19 18:48:18 [info] ... client timed out (110: Connection timed out) while SSL handshaking, client: 172.70.xxx.xxx, server: 0.0.0.0:443
2026/11/19 20:19:11 [info] ... peer closed connection in SSL handshake (104: Connection reset by peer) while SSL handshaking, client: 162.158.xxx.xxx, server: 0.0.0.0:443
2026/11/19 21:17:59 [info] ... client timed out (110: Connection timed out) while SSL handshaking, client: 162.158.xxx.xxx, server: 0.0.0.0:443
2026/11/19 23:17:55 [info] ... client timed out (110: Connection timed out) while SSL handshaking, client: 104.22.xxx.xxx, server: 0.0.0.0:443
2026/11/20 01:47:22 [info] ... client timed out (110: Connection timed out) while SSL handshaking, client: 172.68.xxx.xxx, server: 0.0.0.0:443

时间线完全吻合(Nginx 默认 ssl_handshake_timeout 为 60 秒):

  • CF 21:16:53 报 525 → 60s 后 Nginx 21:17:59 记录 SSL 握手超时
  • CF 23:16:49 报 525 / 23:17:00 报 522 → 60s 后 Nginx 23:17:55 记录 SSL 握手超时
  • CF 01:46:21 报 522 → 60s 后 Nginx 01:47:22 记录 SSL 握手超时

第三阶段:排查 EC2 网卡与 AWS ENA 限流

检查本机主网卡丢包及 AWS ENA 突发限制指标(已脱敏):

1
2
3
IFACE=$(ip route show default | awk '{print $5; exit}')
ip -s link show "$IFACE"
ethtool -S "$IFACE" | grep -iE 'drop|err|exceeded|fail'

输出核心指标:

1
2
3
4
5
6
7
RX: packets 138376345  errors 0  dropped 0  missed 0
TX: packets 113358962 errors 0 dropped 0
bw_in_allowance_exceeded: 0
bw_out_allowance_exceeded: 0
pps_allowance_exceeded: 0
conntrack_allowance_exceeded: 0
linklocal_allowance_exceeded: 0

结论:EC2 实例自身网卡、AWS ENA 配额、Nginx 与后端应用均完全正常,无任何丢包或限流。


3. 根因总结

状态码 归因分析
522 / 525 主要调用方在新加坡(SG),源站在美国俄亥俄(us-east-2)。北京时间夜间国际公网出口拥堵时,Cloudflare 边缘节点 ↔ AWS us-east-2 跨太平洋回源链路出现间歇性丢包:TCP SYN 丢包触发 522,TLS 握手包丢包触发 525(Nginx 侧表现为 60s 后 while SSL handshaking 超时)。
520 / 524 与默认 keepalive_timeout 65 引起的连接复用竞争及跨洋链路抖动有关,调大至 180 后已消除。
499 客户端在等待响应期间主动断开连接(需检查调用方 HTTP 客户端超时配置)。

4. 优化措施与后续方案

已实施:1. 开启 Cloudflare Argo Smart Routing

在 Cloudflare 开启 Argo Smart Routing,使回源流量走 CF 私有骨干网直达源站,避开拥堵的跨洋公网链路。

已实施:2. 开启 EC2 TCP BBR 拥塞控制

Linux 默认 cubic 在跨洋高延迟丢包场景下恢复较慢,已切换为 fq + bbr 以提升跨洋重传与握手恢复能力:

1
2
3
4
5
6
7
8
9
10
11
12
13
sysctl net.ipv4.tcp_congestion_control   # 原为 cubic

modprobe tcp_bbr
cat <<EOF > /etc/sysctl.d/99-bbr.conf
net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr
EOF

sysctl --system
sysctl net.core.default_qdisc net.ipv4.tcp_congestion_control
# 输出:
# net.core.default_qdisc = fq
# net.ipv4.tcp_congestion_control = bbr

后续观察与长期方案

  1. 短期观察:观察开启 Argo + BBR 后晚高峰 522 / 525 出现频率;排查完成后将 Nginx error_log 恢复为 error 级别,避免 info 日志占用磁盘。
  2. 长期架构方案:若主要业务调用方长期位于亚太地区,建议将源站迁往距离更近的可用区(如 **AWS ap-southeast-1**)。