I am trying to figure out a simple way of writing an nginx proxy_pass condition where depending on the URL called upon (e.g. /go/192.168.0.10) the proxy_pass would take the IP after the /go/ and proxy to HTTP://192.168.0.10:8080/stats.
Basically what I am trying to do is avoid having to write hundreds of individual proxy_pass statements like this
location /go/192.168.0.10 {
proxy_pass http://192.168.0.10:8080/stats;
}
To give context, I have a machine to which I have access, this machine, in turn, can reach multiple remote devices via VPN behind it, and I wish to proxy the web interfaces via the machine I have access to.
Thank you for taking the time to read this and for any assistance you provided.
I am trying to figure out a simple way of writing an nginx proxy_pass condition where depending on the URL called upon (e.g. /go/192.168.0.10) the proxy_pass would take the IP after the /go/ and proxy to HTTP://192.168.0.10:8080/stats.
Basically what I am trying to do is avoid having to write hundreds of individual proxy_pass statements like this
location /go/192.168.0.10 {
proxy_pass http://192.168.0.10:8080/stats;
}
To give context, I have a machine to which I have access, this machine, in turn, can reach multiple remote devices via VPN behind it, and I wish to proxy the web interfaces via the machine I have access to.
Thank you for taking the time to read this and for any assistance you provided.
Share Improve this question edited Feb 6 at 16:14 kasonne asked Feb 6 at 16:09 kasonnekasonne 234 bronze badges1 Answer
Reset to default 0You can use a regex location for this:
location ~ "^/go/(\d{1,3}(?:\.\d{1,3}){3})$" {
proxy_pass http://$1:8080/stats;
}
You can even allow specifying a URI after the device IP:
location ~ "^/go/(\d{1,3}(?:\.\d{1,3}){3})(/.*)?" {
proxy_pass http://$1:8080$2;
}
In case you want to pass a query string too:
location ~ "^/go/(\d{1,3}(?:\.\d{1,3}){3})(/.*)?" {
proxy_pass http://$1:8080$2$is_args$args;
}