resolver

The 'resolver' directive in NGINX enables DNS resolution for proxied requests using an external resolver.

Syntaxresolver address [address ...] [valid=duration] [timeout=duration];
Defaultnone
Context
Arguments1+

Description

The 'resolver' directive configures the DNS resolver that NGINX uses to resolve hostnames when processing client requests. This directive can accept one or more DNS server addresses, which can be specified in both IPv4 and IPv6 formats. The resolver may be specified in either the main or a server context, and NGINX will use the defined resolver to resolve any hostnames specified in directives like 'proxy_pass' or 'fastcgi_pass'. When multiple resolvers are provided, NGINX will attempt to resolve hostnames using them in the order specified. If the first resolver fails, it will proceed to the next one. This allows for robustness and flexibility in DNS resolution, particularly in environments where redundancy is crucial.

Additionally, the resolver directive can accept optional parameters such as 'valid' and 'timeout', which control how long the resolved address remains valid and the timeout duration for DNS queries respectively. These parameters help to fine-tune the behavior of the DNS resolution process, ensuring high availability and performance for applications that rely on hostname resolution during runtime. If no resolver is defined, and a hostname resolution is required, NGINX will respond with an error, making this directive critical for configurations that depend on dynamic backend hosts.

Config Example

resolver 8.8.8.8 8.8.4.4;  
location /app {  
    proxy_pass http://mybackend;  
}

Ensure that the specified resolver addresses are reachable from your NGINX server; otherwise, name resolution will fail.

Using a DNS resolver that isn't well-configured can lead to latency issues and slow response times for your application.

If no default resolver is set and you attempt to resolve a hostname, NGINX will return a '502 Bad Gateway' error.

← Back to all directives