Categories
SysOps

How to take the advantage of dynamic DNS resolution in NGINX

Take the advantage of dynamic DNS resolution in an open-source version of NGINX.

NGINX version.

nginx  -v
nginx version: nginx/1.18.0

The easiest method to use dynamic DNS resolution is to define single or multiple DNS servers using a resolve directive and the time the server will cache an answer. Then use a custom variable to define the backend, so the web-server will re‑resolve the domain name when its cache expires.

server {
  server_name _;

  resolver 172.16.0.1:53 valid=5s;

  location / {
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    set $backend http://dynamic-backend.example.org:8000;
    proxy_pass $backend;
  }
}

In this example the IP address for dynamic-backend.example.org will be queried every five seconds, but do not worry about NXDomain replies as these will not be cached.

Keep it simple, stupid.

Additional notes

Module ngx_http_core_module

Using DNS for Service Discovery with NGINX and NGINX Plus