Nginx反向代理DNS缓存问题

Nginx反向代理DNS缓存问题

内网环境通过Nginx反向代理访问外网,Nginx配置完成,启动后的一段时间内接口访问正常,一段时间之后,访问拒绝。./nginx -s reload 重新加载,或者./nginx -s stop./nginx -c nginx.conf重新启动后,接口访问正常,一段时间后又访问拒绝。

内网环境通过代理访问外网,由于DNS缓存IP地址信息,服务端(高德相关接口)IP切换,或者服务动态上下线,导致DNS缓存的IP地址不可用,从而访问拒绝 404

curl -H “Content-Type: application/json” -X POST -d ‘{“origins”:“118.2316,32.5033”,“destination”:“118.2249,32.4972”,“output”:“JSON”,“key”:“dd310ade0c3789edf5d066bfc89f3383”,“type”:“0”}’ “https://restapi.amap.com/v3/distance?origins=118.2316,32.5033&destination=118.2249,32.4972&output=JSON&key=dd310ade0c3789edf5d066bfc89f3383&type=0”

正常响应

{“info”:“INVALID_USER_IP”,“infocode”:“10005”,“status”:“0”,“sec_code_debug”:“d41d8cd98f00b204e9800998ecf8427e”,“key”:“dd310ade0c3789edf5d066bfc89f3383”,“sec_code”:“d41d8cd98f00b204e9800998ecf8427e”}

access.log
日志打印其他代理接口访问正常
error.log
访问拒绝

[error] 30749#0: 23011 connection() failed (111: connection refused) while connecting to upstream,client 10.0.xxx.xxx,server: , request:“POST /v3/geocode/geo?address=xxxxxxxxxxxxxx HTTP/1.1”, upstream: "
https://106.11.43.113:443/v3/geocode/geo?address=xxxxx&output=xxxxxx&key=xxxxx", host: “10.4.xx.xxx:10055”

正在连接106.11.43.113…无法打开到主机的连接。 在端口 443: 连接失败

描述:现在已经明确网络没有问题,代理正常启动,接口联通正常;出现404的原因在于域名解析到的IP不可用。
解决:Nginx所使用的的DNS缓存时间是否可以调整,或者说失败到达一定次数之后尝试解析新的IP

server { listen 10055; server_name localhost; resolver 114.114.114.114 8.8.8.8 valid=1800s; resolver_timeout 3s; set $amap"restapi.amap.com; location / { proxy_pass http://$amap; proxy_buffers 256 4k; proxy_max_temp_file_size 0k; proxy_connect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60; proxy_next_upstream error timeout invalid_header http_502; } } 

上述示例,缓存时间:valid=1800s,如果服务端动态切换比较频繁,那么需要继续缩小 valid

upstream amap{ least_conn; server restapi.amap.com:443 max_fails=3 fail_timeout=10s; } server { location / { proxy_pass http://amap; proxy_buffers 256 4k; proxy_max_temp_file_size 0k; proxy_connect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60; proxy_next_upstream error timeout invalid_header http_502; } } 

以上两种均在不同场景下解决Nginx本地域名解析缓存IP,导致访问拒绝的问题。

原文链接:https://blog.csdn.net/shang_xs/article/details/118114838

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享