nginx 动态解析之 resolver

默认情况下,upstream 和 proxy_pass 仅在 nginx 启动时解析,如果更改解析的IP地址,Nginx仍将使用旧的 IP 地址,直到 NGINX 重新加载或重新启动

无效域名 将无法启动 

upstream localht_upstream {
     server www.exe.com; 
}

或者

proxy_pass http://www.exe.com;

将提示

nginx: [emerg] host not found in upstream "www.exe.com" 

官方商业版本支持定期健康检查功能

Dynamically configurable group with periodic health checks is available as part of our commercial subscription

resolver 10.0.0.1;
upstream dynamic {
    zone upstream_dynamic 64k;
    server backend1.example.com      weight=5;
    server backend2.example.com:8080 fail_timeout=5s slow_start=30s;
    server 192.0.2.1                 max_fails=3;
    server backend3.example.com      resolve;
    server backend4.example.com      service=http resolve;
    server backup1.example.com:8080  backup;
}

 其他方案

  • proxy_pass + resolver:如果您只需要代理到 1 个域并且不需要上游的附加功能,则 nginxproxy_pass可以在运行时执行解析。
  • ngx_upstream_jdomain:一个异步解析域名的 nginx 模块。jdomain 与此模块的主要区别在于,即使没有生成服务器流量,此模块也会保持域名最新(jdomain 需要每个上游的流量才能保持最新)。如果给出了无法解析的域名,此模块还允许 nginx 启动。
  • tengine 的 dynamic_resolve:如果您正在使用 tengine (一个 nginx 分支),那么有一个新功能(当前未发布)支持在运行时解析上游的域名。
  • nginx-upstream-dynamic-servers
 

resolver配置的影响 

  • NGINX 配置中使用resolver指令和指令中的变量proxy_pass会减慢请求处理速度,因为它将成为请求处理中的动态 DNS 解析的额外步骤。
  • NGINX 在域名的有效期 (TTL) 到期时重新解析该域名。通过将参数添加valid到resolver指令中,您可以告诉 NGINX 忽略 TTL 并以指定的频率重新解析名称。
  • 如果 DNS 服务器宕机,NGINX 将无法处理解析。

proxy_pass + resolver 方案配置

//nginx.conf  全局配置
    resolver 223.5.5.5 valid=10s;
    resolver_timeout 10s;


#Syntax:	resolver_timeout time;
#Default:	
#resolver_timeout 30s;
#Context:	http, server, location
#Sets a timeout for name resolution

#valid:By default, nginx caches answers using the TTL value of a response. An optional valid parameter allows overriding it:

转载于:

   https://docs.wallarm.com/admin-en/configure-dynamic-dns-resolution-nginx/

   https://github.com/GUI/nginx-upstream-dynamic-servers/blob/master/README.md

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值