Proxy a URL to a dynamically set port

linux-networkingPROXYreverse-proxy

Say I have URL=www.mysite.com/20000/

I want to get a proxy that can then direct traffic to www.mysite.com:20000. The catch is I don't know what the URL's/ports will be ahead of time. So I can't statically match url /20000/ to port 20000 in the proxy config. Is there a program I could use that I could save a regx from the URL to a variable and set that to the port I want to proxy? Or is this not possible. For context this is a front-end to view SSH connections that come in on random ports. Thanks

Best Answer

Word of caution:

The solution below can be a major security issue because you allow the end user to specify a port to hit on the backend server, hence opening the door to allowing access to ports that shouldn't be visible on the outside.

Some mechanism would have to be in place to prevent unwanted ports - this could also be done in a similar fashion with mod_rewrite to let through anything you want and block what you don't want.


This could be done using Apache httpd mod_rewrite and proxy, the directive would be:

RewriteEngine on
RewriteRule ^/?([0-9]*)/(.*)    http://www.example.com:$1/$2 [L,P]

You will need to enable mod_rewrite and mod_proxy. For full documentation on mod_rewrite, you can consult:

That said, it would probably be better to actually set it up with ProxyPassMatch...

ProxyPassMatch "^/?([0-9]*)/(.*)" "http://backend.example.com:$1/$2"