Dynamic Nginx proxy with Docker

Viewed 571

I'm stucking with configuring a Nginx instance embedded in a Docker container which should implement a dynamic reverse proxy for not-enabled CORS web sites.

I was expecting it was an easy task, but it doesn't work under some conditions. This is a working location block:

location ~* ^/proxy/(.*) {


      proxy_set_header Host            $host;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_redirect off;

      proxy_pass https://google.com;
   }

This configuration works. The google page appears. So it seems Docker is able to resolve google name.

This configuration (which I'm more interested to) doesn't work:

   location ~* ^/proxy/(.*) {

      proxy_set_header Host            $host;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_redirect off;

      proxy_pass http://$1$is_args$args;
   }

It seems that Docker is not able to resolve the name extracted by the first regex group.

If I add in the location block the resolver directive it starts working.

      location ~* ^/proxy/(.*) {
        resolver 192.168.31.2;
        proxy_set_header Host            $host;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_redirect off;

        proxy_pass http://$1$is_args$args;
   }

So, where's the difference? Why the resolver directive is needed? Why in the first case (if the proxy path name is hardcoded) everything's working while not in the other case? Is the host resolv.conf file should be used inside the container itself?

I also tried to create the container passing the --dns option but still not working.

Ideas? Thanks, Fb

1 Answers

Nginx try to resolve domain_name with upstream directive: if it fail it will try to use resolver to solve your name as DNS. So, in the end, you need to set resolver directive.

Related