Runtime DNS resolution limits when using variables in proxy_pass
0 reputation · 02 Feb 2023, 15:13 UTC
0 reputation · 02 Feb 2023, 15:13 UTC
In open-source nginx, achieving runtime DNS resolution for upstream targets typically requires using a variable within the proxy_pass directive combined with a resolver configuration. This approach prevents the server from resolving the hostname only once at startup.
However, shifting to variable-based resolution alters the core behavior of the proxy module. Specifically, this method bypasses the upstream block entirely, which removes the ability to utilize native load balancing algorithms and persistent keepalive connections to the backend servers.
When designing for high-availability environments where upstream IPs change frequently, there is a trade-off between DNS agility and connection performance.
valid parameter is omitted from the resolver directive?1. Caching when valid is omitted
valid is not set, the record’s TTL becomes the cache lifetime.2. Latency when resolver_timeout is hit
resolver_timeout duration (default 30 s) while waiting for a DNS reply.502 Bad Gateway to the client.Verify the NGINX version to ensure the caching behaviour matches the documented implementation:
nginx -vCheck the resolver configuration in nginx.conf or the relevant server block:
resolver 10.0.0.53; # without valid
# or
resolver 10.0.0.53 valid=30s;To observe caching, enable DNS query logging on the resolver or capture traffic with tcpdump -i eth0 port 53 and watch that subsequent requests within the TTL do not trigger new queries.
To test latency impact, point resolver at an unreachable address and measure the response time with curl -w "%{time_total}\n" -o /dev/null -s http://example.com. The duration should match the configured resolver_timeout before a 502 is returned.
In production, consider lowering resolver_timeout to 2–5 s and using a local caching resolver to bound worst‑case latency.
Could you confirm which NGINX version you are running? The caching and stale‑answer behaviour changed subtly in 1.21 and later, so knowing the exact release will help tailor the recommendation.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.