如何强制nginx每次在执行proxy_pass时parsingDNS(dynamic主机名的)?

我正在使用nginx / 0.7.68,在CentOS上运行,具有以下configuration:

server { listen 80; server_name ***; index index.html index.htm index.php default.html default.htm default.php; location / { root /***; proxy_pass http://***:8888; index index.html index.htm; } # where *** is my variables 

proxy_pass是IP地址经常变化的DNSlogging。 Nginxcaching了过时的IP地址,导致对错误的IP地址的请求。

如何阻止nginxcaching的IP地址,当它是过时的?

这是一个有趣的问题,AFAIK不能很好地工作。 您可以尝试使用上游模块并使用指令进行故障转移,以查看它是否作为黑客工作。

接受的答案在nginx / 1.4.2上不起作用。

proxy_pass设置为variables会强制重新parsingDNS名称,因为Nginx将variables视为不同的静态configuration。

例:

 server { ... resolver 127.0.0.1; set $backend "http://dynamic.example.com:80"; proxy_pass $backend; ... } 

注意:一个parsing器(例如你使用的名字服务器)必须configuration好才能工作。

默认情况下,nginx使用响应的TTL值caching答案。 可选的有效参数可以覆盖它:[2]

 resolver 127.0.0.1 [::1]:5353 valid=30s; 

在版本1.1.9之前,caching时间的调整是不可能的, nginx总是caching5分钟的答案。

资料来源:
[1] http://forum.nginx.org/read.php?2,215830,215832#msg-215832
[2] http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver

在ganbrest评论和ohaal答案中有宝贵的信息。

但是我认为重要的是要提到2016年发布的这个官方的nginx文章,它清楚地解释了nginx在这个问题上的行为和可能的解决scheme: https : //www.nginx.com/blog/dns-service-discovery-nginx-plus /

我们确实必须“在variables中设置域名”并使用parsing器指令。

但是,使用variables会改变重写行为。 您可能必须使用rewrite指令,这取决于您的位置和proxy_pass设置。

PS:会发表评论,但还没有足够的分数…