Apache2 URL Rewrite with Reverse Proxy

apache-2.4mod-proxymod-rewritemod-ssl

I've been trying to accomplish a goal of using Apache as a reverse proxy to translate HTTP to HTTPS as well as using mod_rewrite to inject a query string.

The reverse proxy works if tested without the rewrite. When adding the rewrite however, Apache appears to be looping the query string and appending it multiple times. This actually repeats and spawns a max number of worker threads until I force stop the service.

When I force stop Apache, I get following from my browser:

Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /api/uuid.

Reason: Error reading from remote server

However, the upstream server never appears to see this GET request come through.


My VirtualHost is as follows:

<VirtualHost *:80>
    RewriteEngine On
    RewriteRule ^/api/uuid /api/uuid?var=testing [P,QSA]
    SSLProxyEngine On
    SSLProxyVerify none
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerName off
    SSLProxyCheckPeerExpire off
    ProxyPreserveHost On

    ProxyPass / https://192.168.47.108:8443
    ProxyPassReverse / https://192.168.47.108:8443

</VirtualHost>

Original GET request

http://192.168.47.118/api/uuid?domain=lab.local

Snip from access_log

192.168.47.118 - - [15/Sep/2018:01:27:36 -0500] "GET /api/uuid?var=testing&var=testing&var=testing&var=testing&var=testing&var=testing&var=testing&domain=lab.local HTTP/1.1" 502 395 "-"

Best Answer

Even though I was using [P] which implies [L], and even explicit [L] ([QSA,P,L]), Apache was looping the request.

I made a Rewrite Condition that checked if the var was already in the query.

My working solution:

<VirtualHost *:80>
        RewriteEngine On
        RewriteCond %{QUERY_STRING} !domain
        RewriteRule ^/api/uuid /api/uuid?domain=test.local [P,QSA]
        SSLProxyEngine On
        SSLProxyVerify none
        SSLProxyCheckPeerCN off
        SSLProxyCheckPeerName off
        SSLProxyCheckPeerExpire off
        ProxyPreserveHost On

        ProxyPass / https://192.168.47.108:8443/
        ProxyPassReverse / https://192.168.47.108:8443/

</VirtualHost>
Related Topic