Nginx proxy_pass with variable - full path

I originally had a conf like this:

location /some/path/ {
  proxy_pass       http://other.host/foo/;
}

      

And requests to http://my.domain/some/path/bar

will be http://my.domain/some/path/bar

onhttp://other.host/foo/bar

I started using variables in proxy_pass to force nginx to re-resolve DNS:

location /some/path/ {
  resolver        1.2.3.4;
  set $proxy_root  "other.host/foo"
  proxy_pass       http://$proxy_root/;
}

      

But I found that the rest of the uri path is no longer appended, so now requests to http://my.domain/some/path/bar

will http://my.domain/some/path/bar

just be on http://other.host/foo/

.

So I changed it to regex

location ~ ^/some/path/(.*) {
  resolver        1.2.3.4;
  set $proxy_root  "other.host/foo"
  proxy_pass       http://$proxy_root/$1;
}

      

But that doesn't include any query parameters, so I updated again

location ~ ^/some/path/(.*) {
  resolver        1.2.3.4;
  set $proxy_root  "other.host/foo"
  proxy_pass       http://$proxy_root/$1?$args;
}

      

It seems to work, but does it mean? in each target address when only some of the incoming requests actually have a? query ...

I think I could do some further string manipulation, but this feels a bit more. Is there an easier way to proxy_pass as I did originally, but with the goal of assigning the proxy as a variable to force re-permission?

+3


source to share





All Articles