最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

nginx - Dynamic Proxy Pass Configuration - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

You 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;
}
发布评论

评论列表(0)

  1. 暂无评论