Dynamic DNS Resolve

From SEnginx
Jump to: navigation, search


Dynamic DNS Resolve in Upstream


This feature enhances the DNS resolving functionality of upstream module in original nginx. Original nginx resolves the domain name in proxy_pass or upstream block only once at the configuration parsing phase. If the addresses of a domain name is changed, in order to take effect, users need to reload or restart nginx.
SEnginx can support resolving DNS at run time.

This feature is availiable since version 1.5.10


The dynamic DNS resolving features adds some new parameters for proxy_pass directive: dynamic_resolve, dynamic_fail_timeout, dynamic_fallback.
If dynamic_resolve is specified, the dynamic resolving function is enabled for this proxy_pass. This feature requires to work along with nginx's resolver feature. If no resolver is defined, the dynamic resolving function does not have effect.

resolver valid=1;
resolver_timeout 1s;
location / {
    proxy_pass https://senginx.org dynamic_resolve dynamic_fail_timeout=30s dynamic_fallback=stale;

dynamic_fail_timeout and dynamic_fallback parameters are valid since 1.5.14 version.

  • dynamic_fail_timeout is used to set the no-querying period since last failed DNS query.
  • dynamic_fallback specifies the fallback action if a DNS query is failed:
    • next, use next upstream, this the default value
    • stale, use stale DNS response data, usually it is the result from the last successful DNS response.
    • shutdown, shutdown the connection from client directly.

In some scenarios, even the dynamic_resolve parameter is specified, the feature will not be functional, which includes:

  1. The first parameter of proxy_pass is a name of upstream and all servers in that upstream are IP addresses;
  2. The first parameter of proxy_pass is an URL and the host part of this URL is a variable. In this scenario, nginx will resolve the host dynamically by default.

Except above scenarios, the dynamic resolving will be work properly but only with some specified load balancing algorithm. Currently all algorithms provided by SEnginx are supported, including:

  • round_robin
  • least_conn
  • fastest
  • fair
  • ip_hash

Thost 3rd party algorithms which are not included in this list are not supported to work with dynamic resolving feature. If you want to use other algorithms, you need to modify them to support dnamic resolving.
In addition, this feature is limited to use the first IP address in DNS response at current stage, even a DNS server returns a list of IP addresses.

Configuration Examples

proxy_pass to an upstream

upstream backend {
    server host1.domain1;
    server host2.domain2;

resolver valid=1;
resolver timeout 1s;

server {
    location /static_dns {
        proxy_pass http://backend;

    location /dynamic_dns {
        proxy_pass http://backend dynamic_resolve;

host1.domain1 and host2.domain2 will be resolved into IP addresses and saved in upstream's configuration when nginx is parsing the configuration file. When the IP addresses of those 2 domains have been changed, requests to /static_dns will be failed because senginx will still try to connect to the old addresses, but requests to /dynamic_dns will be proxied to new addresses due to dynamic dns resolve feature.

proxy_pass to an URL

resolver valid=1;
resolver timeout 1s;

server {
    location /static_dns {
        proxy_pass http://host1.domain1;

    location /dynamic_dns {
        proxy_pass http://host2.domain2 dynamic_resolve;

host1.domain1 is only resolved into IP address at starting up or reloading phase of nginx. host2.domain2 will be resolved into IP address at run time, si if it's addresses is changed, SEnginx will proxied the requests to new addresses.

Retrieved from "https://senginx.org/en/index.php?title=Dynamic_DNS_Resolve&oldid=7958"
Personal tools

In other languages
  • 中文
  • What links here
  • Related changes
  • Special pages
  • Printable version