When ProxyPass and ProxyPassReverse aren't enough

If you’ve ever used ProxyPass and ProxyPassReverse you know how awesomely powerful they are when integrating multiple backends and keeping the things neat and tidy in Apache. Recently I had to integrate with a backend app using Apache as a reverse proxy. Ideally, and how I’ve done it a million times in the past, you are able to have the backend application respond on a particular host/port/path ( i.e. http://foo.example.com:1234/backend ). This makes setting up Apache via ProxyPass and ProxyPassReverse a snap.

1
2
ProxyPass        /backend/ http://foo.example.com:1234/backend/
ProxyPassReverse /backend/ http://foo.example.com:1234/backend/

So what happens when you can’t run the backend application on a specific context root? By that I mean it can only run at: http://foo.example.com:1234/. This is where mod_proxy_html comes in. In a nutshell, mod_proxy_html allows you to rewrite html, javascript & css so that the URLs can cleanly go through your reverse proxy. This means that the backend application responds with

1
<script src="/script/application.js" type="text/javascript"></script>

mod_proxy_html will converted it to

1
<script src="/backend/script/application.js" type="text/javascript"></script>

To get this to work, add the following to httpd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
Include conf.d/proxy_html.conf

ProxyPass /backend/ http://foo.example.com:1234/
ProxyPassReverse /backend/ http://foo.example.com:1234/
ProxyHTMLURLMap http://foo.example.com:1234 /backend

<Location /backend/>
ProxyHTMLEnable On
ProxyPassReverse http://foo.example.com:1234/
SetOutputFilter proxy-html
ProxyHTMLURLMap / /backend/
ProxyHTMLURLMap /backend /backend
</Location>

mod_proxy_html is available in Apache 2.4, and as a 3rd party installable module in older Apache 2.x. I needed it in Apache 2.2 on Centos 6 and used the RPM from epel. This post just scratches the surface with what you can do with mod_proxy_html. There are a ton of options for configuring meta headers, html fixups, and buffer sizes. Go check it out!