When configuring the Nginx resolver, specifying multiple name servers in the resolver directive is a common practice to ensure redundancy and reliability. This setup allows Nginx to continue resolving domain names even if one or more DNS servers become unavailable.
How Does the Nginx Resolver Work with Multiple Name Servers?
When you configure multiple name servers in the resolver directive, Nginx follows a specific behavior to query these servers:
- Round-Robin Querying: Nginx uses a round-robin approach to query the DNS servers. This means that Nginx will sequentially query the list of name servers you specified. If the first server fails to respond, Nginx will try the next server in the list, and so on.
- Failover Mechanism: If a DNS query to one server fails (e.g., due to timeout or error), Nginx will automatically retry the query with the next server in the list. This process continues until Nginx either gets a valid response or exhausts all configured servers.
- Caching and Validity: Once Nginx receives a valid DNS response, it caches the result for the duration specified by the
validparameter. During this caching period, Nginx will use the cached response and won’t query the DNS servers again for the same domain name.
Example Configuration with Multiple Name Servers
Here is an example of configuring Nginx with multiple name servers in the resolver directive:
nginxCopy codehttp {
resolver 8.8.8.8 8.8.4.4 1.1.1.1 valid=300s ipv6=off;
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend.example.com;
}
}
}
In this example:
- Name Servers: Three DNS servers are specified:
8.8.8.8,8.8.4.4, and1.1.1.1. - TTL (valid): DNS responses are cached for 300 seconds.
- IPv6: IPv6 resolution is disabled.
Detailed Behavior
- Initial DNS Query: When a request is made to
example.comthat requires resolvingbackend.example.com:- Nginx sends a DNS query to the first server in the list (
8.8.8.8). - If
8.8.8.8responds with a valid IP address, Nginx caches the response and uses the IP address to forward the request. - If
8.8.8.8does not respond (due to timeout or error), Nginx retries the query with the next server in the list (8.8.4.4), and so on.
- Nginx sends a DNS query to the first server in the list (
- Cache Usage: For subsequent requests within the 300-second validity period:
- Nginx uses the cached IP address without querying the DNS servers again.
- This reduces latency and DNS query load.
- Failover Handling: If a DNS server becomes temporarily unavailable:
- Nginx will seamlessly use the next available server in the list for new DNS queries.
- This ensures continuous resolution capability.
Benefits of Using Multiple Name Servers
- Redundancy: If one DNS server fails, others are available to provide DNS resolution, ensuring high availability.
- Load Distribution: The round-robin querying approach distributes DNS query load across multiple servers, potentially improving resolution performance.
- Resilience: In dynamic and high-traffic environments, having multiple DNS servers minimizes the risk of resolution failures, contributing to overall system resilience.
Best Practices
- Specify Reliable DNS Servers: Use well-known and reliable DNS servers (e.g., Google DNS, Cloudflare DNS, etc.).nginxCopy code
resolver 8.8.8.8 8.8.4.4 1.1.1.1 valid=300s; - Monitor DNS Server Availability: Regularly monitor the availability and performance of your configured DNS servers. Adjust the list as necessary based on their reliability.
- Appropriate TTL Configuration: Set a TTL (
valid) that balances between fresh DNS data and query load. Shorter TTLs ensure up-to-date IP addresses but increase DNS query frequency. Longer TTLs reduce query load but may use outdated IP addresses longer.nginxCopy coderesolver 8.8.8.8 8.8.4.4 valid=300s;
Conclusion
Configuring the Nginx resolver with multiple name servers enhances the robustness and reliability of DNS resolution in your Nginx setup. By understanding the round-robin querying mechanism, failover handling, and caching behavior, you can optimize your Nginx configuration for high availability and performance. Following best practices ensures your web server can handle DNS lookups efficiently, even in dynamic and high-traffic environments.

Leave a comment