Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse proxy a dynamic port with Apache?

I make a request to this URL: wss://domain/websockify?port=6801 The port parameter can change dynamically. How could I make ProxyPass to redirect to a dynamic port extracted from the URL?

This is the configuration that works with a predefined port:

ProxyPass /websockify ws://localhost:6801/websockify/
ProxyPassReverse /websockify ws://localhost:6801/websockify/

If I use ProxyPassMatch the apache tells me there is an error:

ProxyPassMatch ^/.*\?port=(.*)$ ws://localhost:$1/websockify/

#ProxyPass Unable to parse URL: ws://localhost:$1/websockify/

I use apache 2.4.10

like image 884
robert Avatar asked Apr 24 '26 06:04

robert


1 Answers

This needs to be done with mod_rewrite.

Match the port from the query string. Redirect to the port that we matched in the condition with the [P] flag. Use the same match in the ProxyPassRevese.

RewriteEngine on
RewriteCond %{QUERY_STRING} port=(.*)
RewriteRule /websockify ws://localhost:%1/websockify/ [P]
ProxyPassReverse /websockify ws://localhost:%1/websockify/
like image 84
robert Avatar answered Apr 27 '26 10:04

robert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!