'How to Redirection and apply TLS with single web server multiple ports using RewriteEngine and ProxyPassReverse in Apache configuration

I'm running into a very serious problem. Hope someone kind can help.

Please see the code below first. This is part of my Apache configuration.


First, the URL of the target web server is http://localhost:30001 ~ 30010. Identifies the site to be accessed through each PORT.

When I receive input as "http://localhost/wksp/30001", I use RewriteEngine to make it "http://localhost:30001". This part works without any problems.

However, I tried to apply TLS and run "https" through ProxyPass and ProxyPassReverse again, but it is very difficult.

From what I understand, ProxyPassReverse accesses "http://localhost:[PORT]/" at first, but then communicates with Proxy through "https://localhost/".

Therefore, when communicating with the https protocol, the PORT cannot be dynamically acquired. For example, if http://localhost:30001 is the target, you need to communicate with the proxy server through port 30001.

In my code now, when HTTPS=on, PORT is written as 30001, but what I want is to keep holding the variable $1 when HTTPS=off.

If you have another approach, that's fine. lots of advice Thank.

<VirtualHost *:443>

...

SSLEngine On

ProxyPreserveHost On
SSLProxyEngine On
ProxyRequests Off
RequestHeader set Front-End-Https "On"

...

RewriteEngine On
RewriteRule ^/wksp/(.*) http://localhost:$1 [P]
ProxyPassReverse / http://localhost:$1

<Location />
Order allow,deny
Allow from all

RewriteEngine On

RewriteCond "%{HTTPS}" =off
RewriteRule ^:(.*) http://localhost:$1/ [P]
ProxyPassReverse http://localhost:$1/

RewriteCond "%{HTTPS}" =on
ProxyPass http://localhost:30001/
ProxyPassReverse http://localhost:30001/
</Location>

</VirtualHost>

If HTTPS=on, if you change the url port of ProxyPass and ProxyPassReverse to $1, the following error is displayed.

httpd[1584880]: AH00526: Syntax error on line 55 of /etc/httpd/conf.d/wksp-tls.conf:
ProxyPass Unable to parse URL: http://localhost:$1/


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source