I want to resolve DNS every time when a new request will come, I am trying to resolve DNS dynamically in Nginx by setting DNS in a variable. It is working correctly for the below example.
server {
location /mypath {
resolver 10.0.3.2 10.0.2.2 ipv6=off valid=10s;
resolver_timeout 30s;
set $backend_servers backends.example.com;
proxy_pass http://$backend_servers/mypath;
}
}
but when I have multiple Location blocks with the same DNS name in the variable, it is not able to resolve the DNS.
server {
location /mypath {
resolver 10.0.3.2 10.0.2.2 ipv6=off valid=10s;
resolver_timeout 30s;
set $backend_servers backends.example.com;
proxy_pass http://$backend_servers/mypath;
}
location /mysecondpath {
resolver 10.0.3.2 10.0.2.2 ipv6=off valid=10s;
resolver_timeout 30s;
set $backend_servers backends.example.com;
proxy_pass http://$backend_servers/mysecondpath;
}
}
If you observe $backend_servers is used in both location block. if I use any one of them only Nginx proxy works perfectly. when I use both it returns 502.
What I am doing wrong here? Thanks in advance.