20

I have created a proxy pass for multi URLs.

    listen 80;
    listen [::]:80;

    server_name ~^(.*)redzilla\.11\.75\.65\.21\.xip\.io$;

            location / {
                    set $instname $1;
                    proxy_pass http://${instname}redzilla.localhost:3000;
            }

When I call to this service using chrome, It was triggered 502 error.

http://test.redzilla.11.75.65.21.xip.io/

I put below location tag by hard coding the URL.

            location /redzilla {
                    proxy_pass http://test.redzilla.localhost:3000;
            }

Then It is working for only above URL. I want to know how to create proxy pass for multiple URL within single location tag. ( please note : URL pattern is *.redzilla.localhost:3000 , * ( star ) represent any word)

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • You will need to define a [`resolver` statement](http://nginx.org/en/docs/http/ngx_http_core_module.html#resolver). – Richard Smith Sep 14 '19 at 19:18
  • 3
    Why is a resolver statement needed in this situation (over the normal situation)? – Joe Sep 25 '19 at 15:05
  • @Joe: If I’m not too mistaken, it’s because this time nginx really needs to resolve the address: it’s not passed to the browser to do the resolving, but nginx itself fetches the content. Usually, when doing a proxy, you use a unix socket or an ip directly. – Smar Mar 25 '20 at 05:49
  • I'm stuck on this issue too. Did you ever figure it out? – Thom Nov 18 '20 at 13:35
  • Same issue in a docker container – AV8R Nov 21 '20 at 21:54
  • 2
    Same error in docker. Without using regex match and specifying it statically works well. Solution by snez (adding "resolver 127.0.0.11;") works fine. Seems like some nginx implementation-specifics to me. – adabru Jun 04 '21 at 09:52
  • looks like a nginx bug to me. I created https://trac.nginx.org/nginx/ticket/2335#ticket let's see what happens ;) – StefanKaerst Mar 15 '22 at 12:26

2 Answers2

14

If you are using nginx inside docker, define a network, using docker network create .... Containers that are part of that network (through the --network flag on docker run), will have a dns resolver added to them, available via 127.0.0.11.

Then in your server {} directive add "resolver 127.0.0.11;"

snez
  • 2,400
  • 23
  • 20
0

If you really need dynamic domain name then you need to use resolver.

However, if you have variables in proxy_pass but NOT in domain name, easiest solution is to define the server using the upstream directive.

See https://nginx.org/en/docs/http/ngx_http_upstream_module.html

Example:

upstream backend {
    server backend.example.com;
}

server {
    location /(?<uri_match_prefix>.*)$ {
        proxy_pass http://backend/$uri_match_prefix$is_args$args;
    }
}
Honza
  • 499
  • 4
  • 12