DNS cache in nginx

Maxim Dounin mdounin at mdounin.ru
Mon Jul 6 14:30:03 UTC 2015


Hello!

On Sat, Jul 04, 2015 at 09:42:06AM +0300, Shay Peretz wrote:

> Hello ,
> 
> I have an A record which resolve to 2 CNAME's , the DNS failover will
> make the switch if something go wrong .
> 
> The problem is that after a change the nginx stick to the OLD IP
> unless I reload it ..
> any way the Caching can be disabled ?
> I tried the following with no success :
> add valid for the resolver ,
> resolver 8.8.8.8 valid=5s;
>   and or
> resolver_timeout 1s;
> 
> Any recommendation to solve it  ?

As long as you write host names in nginx configuration, nginx will 
resolve these names while parsing the configuration.  It won't 
notice any changes unless the configuration is reloaded.

If you want nginx to periodically re-resolve names, there are two 
options available:

1) Use proxy_pass with variables:

   resolver 127.0.0.1;
   set $upstream "backend.example.com";
   proxy_pass http://$upstream;

When variables are used in the "proxy_pass" directive, nginx 
will resolve names at runtime.  See http://nginx.org/r/proxy_pass 
for details.

2) Use the "resolve" flag in an upstream{} block.  Only available 
in nginx plus, see 
http://nginx.org/en/docs/http/ngx_http_upstream_module.html#resolve 
for details.

Alternatively, you may consider using nginx mechanisms to do 
failover instead.  See these links for some details:

http://nginx.org/en/docs/http/ngx_http_upstream_module.html
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx mailing list