在nginx proxy_pass中禁用IPv6

最近在生产服务上遇到 nginx 正向代理连接失败的问题。

1
2
2019/10/22 15:25:21 [error] 1930#0: *1482670 connect() to [AAAA:BBBB:C:DDD:E:F:GGG:HHH]:443 failed (101: Network is unreachable) while connecting to upstream, client: AA.BB.CC.DD, server: example.com, request: "GET /download/file HTTP/1.0", upstream: "https://[AAAA:BBBB:C:DDD:E:F:GGG:HHH]:443/download/file", host: "example.com"

分析错误日志,应答是nginx尝试连接目标的 IPv6 地址端口时失败,错误101: Network is unreachable

服务器没有IPv6地址,那就禁用nginx的IPv6解析

禁用IPv6很简单

1
resolver 8.8.8.8 ipv6=off;