默认情况下,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 个域并且不需要上游的附加功能,则 nginx
proxy_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 将无法处理解析。
//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