# DNS cache in nginx

**URL:** <https://www.ruby-forum.com/t/dns-cache-in-nginx/242580>\
**Category:** NGINX\
**Created:** [July 4, 2015, 8:42am UTC](https://www.ruby-forum.com/t/dns-cache-in-nginx/242580 "2015-07-04T08:42:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [July 4, 2015, 8:42am UTC](https://www.ruby-forum.com/t/dns-cache-in-nginx/242580/1 "2015-07-04T08:42:39Z")

</div>

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 ?

– shay

---

<div class="post-metadata">

**Author:** ![Shay\_Peretz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c67d28/32.png) [@Shay\_Peretz](https://www.ruby-forum.com/u/Shay_Peretz)\
**Post date:** [July 6, 2015, 4:30pm UTC](https://www.ruby-forum.com/t/dns-cache-in-nginx/242580/2 "2015-07-06T16:30:34Z")

</div>

Hello!

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

> 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:

When variables are used in the “proxy\_pass” directive, nginx  
will resolve names at runtime. See [Module ngx\_http\_proxy\_module](http://nginx.org/r/proxy_pass)  
for details.

1. Use the “resolve” flag in an upstream{} block. Only available  
in nginx plus, see  
[Module ngx\_http\_upstream\_module](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:

> **[Module ngx\_http\_upstream\_module](http://nginx.org/en/docs/http/ngx_http_upstream_module.html)**

> **[Module ngx\_http\_proxy\_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_next_upstream)**
>
> The ngx\_http\_proxy\_module module allows passing requests to another server.

–  
Maxim D.

> **[nginx](http://nginx.org/)**
