Search code examples
apacheproxypass

How do I make ProxyPassReverse rewrite relative redirects?


I have trouble getting relative redirects working with Apache and ProxyPassReverse. If I on the back-end server use absolute redirects ie. Location: http://fake.frontend.com/cat, everything is proxied like intended, however if I on the back-end server use relative redirects, ie. Location: /cat it does not work.

I am using Apache 2.4.25 on Debian.

I have the following configuration on the proxy server:

<Location /test/>
    ProxyPass http://fake.backend.local/
    ProxyPreserveHost Off
    ProxyPassReverse http://fake.backend.local/
    SetOutputFilter INFLATE;proxy-html;DEFLATE
    ProxyHTMLURLMap / /cat/
    ProxyHTMLURLMap fake.backend.local/ /cat/
    ProxyHTMLDocType "<!DOCTYPE html>"
</Location>

When I access the proxy server on http://fake.frontend.com/cat/ the back-end server is redirected depending on being signed in or not. If not signed in I am redirected (302) to /Signin. If I am signed in I am redirected (302) to /dashboard. If the back-end application uses absolute paths in the location header, ie: http://fake.backend.local/Signin I then browser loads http://fake.frontend.com/cat/Signin like intended. However, if the backend application users relative paths in the location header, ie: /Signin the browser loads http://fake.frontend.com/Signin (notice the missing cat).

I want to use relative paths on the backend and still get redirected to http://fake.frontend.com/cat/Signin. How can I configure the proxy to work with relative location headers?


Solution

  • I found and article on askapache.com that had an example where one should set the ProxyPassReverse to /.

    Changing to the following configuration solved my problem:

    <Location /test/>
        ProxyPass http://fake.backend.local/
        ProxyPreserveHost Off
        ProxyPassReverse /
        SetOutputFilter INFLATE;proxy-html;DEFLATE
        ProxyHTMLURLMap / /cat/
        ProxyHTMLURLMap fake.backend.local/ /cat/
        ProxyHTMLDocType "<!DOCTYPE html>"
    </Location>