6

So, I want to make dynamic proxy_pass to vagrant machine on my server in .local zone controled by Avahi. I have a nginx virtual host configuration:

server {
        listen 80;
        server_name     ~^(?<subdomain>.+)\.example\.com$;

        location / {

                resolver 127.0.0.1 valid=30s;

                proxy_set_header            Host $http_host;
                proxy_set_header            X-Real-IP $remote_addr;
                proxy_set_header            X-Forwarded-Proto $scheme;
                proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header            Host $host;

                proxy_pass                  http://$subdomain-example.local;
                proxy_connect_timeout       600;
                proxy_send_timeout          600;
                proxy_read_timeout          600;

                send_timeout                600;

        }
    }

Nginx have returned me an error in /var/log/nginx/error.log:

2016/09/26 18:03:34 [error] 24401#24401: 
*1 no resolver defined to resolve test-example.local, 
client: 192.168.1.101, 
server: ~^(?<subdomain>.+)\.example\.com$, 
request: "GET / HTTP/1.1", 
host: "test.example.com"

But, if I manualy set $subdomain to "test", for example, it's work and pass to test-example.local machine as I really want.

How to make it really dynamic? What I should change in my vhost configuration?

NoobTW
  • 2,466
  • 2
  • 24
  • 42

1 Answers1

1

As NGINX starts up or reloads its configuration, it queries a DNS server to resolve domain name. NGINX chooses the DNS server from the OS configuration file /etc/resolv.conf. When you use a variable to specify the domain name in the proxy_pass directive, NGINX does not refer to /etc/resolv.conf.

You can use the IP address in proxy_pass directive instead domain name.

Read https://www.nginx.com/blog/dns-service-discovery-nginx-plus for more details.

Aleksey Vaganov
  • 487
  • 3
  • 9