Nginx tcp traffic forwarding, local DNS server does not take effect

< H2 > nginx tcp traffic forwarding, local DNS server does not take effect < / H2 >

now a simple requirement is to forward tcp traffic through nginx, that is, nginx listens on a port and forwards the traffic to the corresponding domain name and port when a request is received. Since the domain name varies in duration, it needs to be queried through a local DNS.

but after configuring resolver, it is found that nginx does not query the local DNS,. It is still querying the system"s DNS (not the cache problem). If you change the DNS server of the system to the local DNS server address, it can be forwarded normally.

the configuration is shown below, which is actually very simple.

stream {
    resolver 127.0.0.1;
    server {
        listen 8888;
        proxy_pass 1.service.consul:80
    }
}

as shown above, you need to forward the traffic from port 8888 to the ip corresponding to 1.service.consul, and ask for your advice.

Mar.02,2021

proxy_pass variables are needed before nginx initiates DNS requests
so set a variable first

set $consul "1.service.consul";
proxy_pass  $consul:80;
Menu