Error with IP and Nginx as reverse proxy Error with IP and Nginx as reverse proxy nginx nginx

Error with IP and Nginx as reverse proxy


If the proxy_pass value doesn't contain variables, nginx will resolve domain names to IPs while loading the configuration and cache them until you restart/reload it. This is quite understandable from a performance point of view.

But, in case of dynamic DNS record change, this may not be desired. So two options are available depending on the license you possess or not.

Commercial version (Nginx+)

In this case, use an upstream block and specify which domain name need to be resolved periodically using a specific resolver. Records TTL can be overriden using valid=time parameter. The resolve parameter of the server directive will force the DN to be resolved periodically.

http {        resolver X.X.X.X valid=5s;    upstream dynamic {        server foo.dnsalias.net resolve;    }    server {        server_name www.example.com;        location / {            proxy_pass http://dynamic;            ...        }    }}

This feature was added in Nginx+ 1.5.12.

Community version (Nginx)

In that case, you will also need a custom resolver as in the previous solution. But to workaround the unavailable upstream solution, you need to use a variable in your proxy_pass directive. That way nginx will use the resolver too, honoring the caching time specified with the valid parameter. For instance, you can use the domain name as a variable :

http {      resolver X.X.X.X valid=5s;    server {        server_name www.example.com;        set $dn "foo.dnsalias.net";         location / {            proxy_pass http://$dn;            ...        }    }}

Then, you will likely need to add a proxy_redirect directive to handle redirects.


Maybe check this out http://forum.nginx.org/read.php?2,215830,215832#msg-215832

resolver 127.0.0.1;set $backend "foo.example.com";proxy_pass http://$backend;In such setup ip address of "foo.example.com" will be looked updynamically and result will be cached for 5 minutes.