Apache’s reverse proxy stumping me with redirects

apache-2.2PROXYredirectreverse-proxy

I have setting up a series of web apps, which for security purposes are only directly accessible via localhost, with each service attached to a different port. To make these apps externally accessible, a reverse proxy is being setup through apache. It has been running smoothly until adding another app today, which is forwarding after initial access (I don't know by what method though, I do not have access to the code that is doing the redirecting) in a way such that "proxyHost/bar/" is redirecting to "proxyHost/search/" instead of "proxyHost/bar/search/"

I have looked over my script for setting up the proxy multiple times and have tried different approaches I have read about online and none are working for me. I now turn to you guys for help, I am very sorry if this is a rudimentary question or there is an obviously flaw in my config. I am quite new to apache.

Thanks!

Here is the current config:

 ProxyRequests Off
 ProxyPreserveHost On
 ProxyHTMLEnable On

 <Proxy *>
   Order allow,deny
   Allow from all
 </Proxy>

 ProxyPass /foo/ http://localhost:1234/
 ProxyHTMLURLMap http://localhost:1234 /foo

 <Location /foo/>
   ProxyPassReverse  http://localhost:1234/
   ProxyHTMLURLMap /          /foo/
   ProxyHTMLURLMap /foo      /foo
 </Location>

 ProxyPass /bar/ http://localhost:5678/search
 ProxyHTMLURLMap http://localhost:5678/ /bar

 <Location /bar/>
   ProxyPassReverse http://localhost:5678/
   ProxyHTMLURLMap /            /bar/
   ProxyHTMLURLMap /bar     /bar
 </Location>

Best Answer

To restate your problem:

You've installed a new "backend" application and made it accessible under /bar on your frontend host. Access to the application results in a redirect to /search rather than /bar/search.

This generally happens because the backend application doesn't know anything about the frontend path under which you access it, and it generates absolute (vs. relative) URLs. There are a few ways of solving this problem:

  • Some applications let you configure a base URL that they will use when generating absolute links. This is the easiest solution if it's available; you would set the base URL to http://frontendhost/bar.
  • If you are unable to configure or modify the application, then you're going to need to filter the HTML it generates to rewrite links. This is (explicitly) what mod_proxy_html is for. Note that for Apache 2.4 and great, mod_proxy_html is [included][] as a standard module; for earlier versions of Apache you'll need to install it separately.

The mod_proxy_html documentation has some good examples.

Related Topic