Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have nginx/0.7.68 running on my CentOS box with the following 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;

The server that my nginx proxy_pass to is a server with dynamic DNS, whose IP change frequently. The problem is that nginx seems to cache the IP address of the destination of proxy_pass, resulting request to the wrong IP address.

How can I solve this problem?

share|improve this question
looking through the nginx source, it does appear nginx is hardcoded to cache resolves for their TTL - what's the TTL on your dynamic dns? – lunixbochs Feb 26 '11 at 1:52
TTL on my ddns is 60s, the default value of – xiamx Feb 26 '11 at 3:19
See also… – Cheekysoft Apr 13 '15 at 11:48
up vote 4 down vote accepted

It's an intriguing question and AFAIK that's not going to work well. You can try to use the upstream module and use the directives for failover to see if it works as a hack.

share|improve this answer
surprisingly when i changed to upstream, everything worked as expected. I'll then mark this as correct answer – xiamx Feb 27 '11 at 22:20
According to the documentation, there's a special upstream server flag resolve that's only available in the commercial version (see – omribahumi Jul 19 '15 at 13:48

Accepted answer didn't work for me on nginx/1.4.2.

Setting proxy_pass to a variable forces re-resolution of the DNS names (caching it for 5 minutes), because Nginx treats variables differently to static configuration.


server {
    set $backend "";
    proxy_pass $backend;

Note: A resolver (e.g. the name server you use) MUST be configured for this to work.


share|improve this answer
wouldn't this force a dns query on every single request? that sounds like awful performance... – lucascaro Mar 10 '15 at 3:42
No, read the source. In such setup ip address of "" will be looked up dynamically and result will be cached for 5 minutes. I've added it to the answer for clarity. – ohaal Mar 10 '15 at 9:43
After spending most of my day on this - on Ubuntu 12.04 with nginx 1.1.19, set inside location doesn't work properly. Beware – omribahumi Jul 19 '15 at 13:38
This solution worked with me, however I couldn't find a reference for the 5 minutes TTL. By default, nginx caches answers using the TTL value of a response. An optional valid parameter allows overriding it: resolver [::1]:5353 valid=30s; – Montaro Aug 15 '15 at 2:51
It could be wrong, it's based on the source link I provided in my answer. In such setup ip address of "" will be looked up dynamically and result will be cached for 5 minutes. – ohaal Aug 15 '15 at 17:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.