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

  • What proxy technology do you use? Will the /[0-9]*/ always represent the port?
    – ETL
    Sep 20 '15 at 17:10
  • I'm running ubuntu server so I can use anything that will sit on that. Yes /[0-9]*/ will always be the port but I could also do it like mysite.com/?/[0-9]*/. It doesn't really matter I just need to figure out the dynamic part of passing something in the URL to proxying it to that port.
    – Nimjox
    Sep 20 '15 at 18:05

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"
  • 1
    Thanks, I'll note the security but its just for a little side project of mine so if something gets compromised its no big deal. This works.
    – Nimjox
    Sep 20 '15 at 19:25
  • mod_rewrite works, but ProxyPassMatch can not be used because the right side needs to be parsable and generates the following error: AH00526: Syntax error on line XXX of XXX.conf: ProxyPass Unable to parse URL: backend.example.com:$1/$2 I also found that adding the following helped adjust redirects: ProxyPassReverse "/" "backend.example.com:"
    – kjpires
    Oct 15 '20 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.