nginx proxy 代理解析成ipv6導緻請求失敗

2023-04-20 15:20:00
CJL
原創
3287

nginx配置反曏代理後偶爾會有error日誌

2023/04/20 07:18:21 [error] 16202#16202: *158346 connect() to [2001:4860:4802:38]:443 failed (101: Network is unreachable) while connecting to upstream, client: 120.199.83.1, server: a.test.net, request: "GET /new/favicon.ico HTTP/1.1", upstream: "https://[2001:4860:4802:38]:443/new/favicon.ico", host: "a.test.net", referrer: "-"


查詢資料髮現需要禁用ipv6解析

添加resolver 8.8.8.8 ipv6=off valid=600s; 禁用ipv6解析有效期10分鐘


    location / {
            resolver 8.8.8.8 ipv6=off valid=600s;
            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 $scheme;
            proxy_pass https://a.test.net;
    }


nginx -s reload 髮現沒有作用


繼續查詢資料髮現nginx在沒有變量的配置中隻在啟動的時候解析一次域名

(https://trac.nginx.org/nginx/ticket/723)

(https://github.com/DmitryFillo/nginx-proxy-pitfalls)

繼續修改成帶變量的配置如下



    location / {
            set $endpoint a.test.net;
            resolver 8.8.8.8 ipv6=off valid=600s;
            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 $scheme;
            proxy_pass https://${endpoint};
    }


重啟生效







發錶評論
評論通過審核後顯示。
流量統計