This blog post explains the various methods NGINX and NGINX Plus handles domain resolution - https://www.nginx.com/blog/dns-service-discovery-nginx-plus/. To summarize, you can configure NGINX to resolve hostnames regularly using these methods (credits to original blog post):
1. Setting the Domain Name in a Variable
resolver 10.0.0.2 valid=10s;
server {
location / {
set $backend_servers backends.example.com;
proxy_pass http://$backend_servers:8080;
}
}
NGINX re‑resolves the domain name when its TTL expires. You must include the resolver
directive to explicitly specify the name server (NGINX does not refer to /etc/resolv.conf as in the first two methods). By including the
valid
parameter to the resolver
directive, you can tell NGINX to ignore the TTL and re‑resolve names at a specified frequency instead. Here we tell NGINX to re‑resolve names every 10 seconds.
Drawback is that because the upstream group is not used, specify the load‑balancing algorithm or other parameters to the server directive.
2. (NGINX Plus only) Using DNS A records
resolver 10.0.0.2 valid=10s;
upstream backends {
zone backends 64k;
server backends.example.com:8080 resolve;
}
server {
location / {
proxy_pass http://backends;
}
}
By default, NGINX Plus honors the TTL, re‑resolving names when records expire. To have NGINX Plus instead re‑resolve names at a specified frequency, include the valid parameter to the resolver directive.
This gives the additional benefit of being able to configure additional settings for upstream servers via the server directive.
---
Do take a look at the blog post to see other methods available, such as via DNS SRV records which supports dynamic port numbers and weightings. Hope that helps.