Forum Discussion
does nginx (1.20 or newer) re-resolve DNS for proxy_pass?
I've made a set of tests (I use a regular nginx 1.20.1 version, not nginx plus):
1. According to tcpdump - nginx will periodically re-query the DNS for "example.com" if the following config part is used:
location ^~ /_example/ {
proxy_pass https://example.com/_example/;
proxy_set_header Host my-site;
}
However, confusingly - even if DNS changed, the value resolved at nginx startup will be still used!
2. Setting the domain name in the variable - this unfortunately works differently than the "equivalent" without the variable:
a) without the variable:
location ^~ /_example/ {
proxy_pass https://example.com/_example/;
proxy_set_header Host my-site;
}
- connection to local-server/_example/image.jpg will be passed to example.com/_example/image.jpg
- only DNS resolved at startup is used, even though nginx queries DNS for "example.com" from time to time
b) with the variable:
location ^~ /_example/ {
set $backend_server example.com;
proxy_pass https://$backend_server/_example/;
proxy_set_header Host my-site;
}
- connection to local-server/_example/image.jpg will be passed to example.com/_example/ - note the missing "image.jpg" when querying the upstream!
- DNS changes work
On 1, I don't have knowledge of how the underlying code is written, but barring the periodic DNS requests, NGINX seems to be behaving as intended in terms of resolving the host with new value after restart/config reload.
On 2, try changing your location block to
location ^~ /_example/ {
set $backend_server example.com;
proxy_pass https://$backend_server/$request_uri;
proxy_set_header Host my-site;
}
- netsec-groupFeb 07, 2023Nimbostratus
Hi Leon,
one question regarding NGINX (not NGINX+) and setting DNS in a variable.
We are using this method (#1) but having the "set" directive in the server section instead of in the location section. We noticed that NGINX is not updating new IP address ... even if we see DNS requests and responses with updated IP@ (this is happening using AWS ALB).
Is it necessary to have the "set" directive inside the location section instead of in the server location?
Like this:
nginx version: nginx/1.20.2
Thanks for your help,
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com