Apache URL rewriting in reverse proxy

Posted by Jeremy Gooch on Server Fault See other posts from Server Fault or by Jeremy Gooch
Published on 2013-11-08T17:56:32Z Indexed on 2013/11/08 21:58 UTC
Read the original article Hit count: 282

I'm deploying Apache in front of a Karaf-hosted application (Apache and Karaf are on separate servers). I want Apache to operate as a reverse proxy and also to hide part of the URL.

The URL to get the log-in page of the application directly from the app server is http://app-server:8181/jellyfish. Pages are served by the Jetty instance running within Karaf. Of course, this behaviour would usually be blocked by the firewall for everything except the reverse proxy server.

With the firewall off, if you hit this URL then Jetty loads the log-in page. The browser's address bar correctly changes to http://app-server:8181/jellyfish/login?0 and everything works.

What I want is for http://web-server (i.e. from the root) to map to Jetty on the app server with the name of the app (jellyfish) suppressed. e.g. The browser would change to show http://web-server/login?0 in the address bar and all subsequent URLs and content would be served with the web-server's domain and without the jellyfish clutter.

I can get Apache to operate as a simple reverse proxy, using the following config (snippet):-

ProxyPass /jellyfish http://app-server:8181/jellyfish
ProxyPassReverse / http://app-server:8181/

...but this requires the browser's URL to contain jellyfish and going to the root URL (http://web-server) gives a 404 Not Found.

I've spent a lot of time trying to use mod_rewrite with and without its [P] flag to get around this, but without success. I then tried the ProxyPassMatch directive, but I can't seem to get that quite correct either.

Here's the current config, as is loaded into /etc/apache2/sites-available/ on the web server. Note that there is a locally-hosted images directory. I've also kept the mod_rewrite proxy exploit protection and am suppressing a couple of mod_security rules that were giving false positives.

<VirtualHost *:80>
    ServerAdmin admin@drummer-server
    ServerName drummer-server

    ErrorLog ${APACHE_LOG_DIR}/error.log
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /images/ "/var/www/images/"

    RewriteEngine On
    RewriteCond  %{REQUEST_URI}  !^$
    RewriteCond  %{REQUEST_URI}  !^/
    RewriteRule  .*              -    [R=400,L]

    ProxyPass /images !

    ProxyPassMatch ^/(.*) http://granny-server:8181/jellyfish/$1
    ProxyPassReverse / http://granny-server:8181/jellyfish

    ProxyPreserveHost On

    SecRuleRemoveById 981059 981060

    <Directory "/var/www/images">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>

If I go to http://web-server, I get redirected to http://web-server/jellyfish/home but this gives a 404, with a complaint about trying to access /jellyfish/jellyfish/home - NB the browser's address bar does not contain the double /jellyfish.

HTTP ERROR 404

Problem accessing /jellyfish/jellyfish/home. Reason:

    Not Found

And, if I go to http://web-server/login, I get redirected to http://web-server/jellyfish/login?0 but this gives a 404, with a complaint about trying to access /jellyfish/jellyfish/login.

HTTP ERROR 404

Problem accessing /jellyfish/jellyfish/login. Reason:

    Not Found

So, I'm guessing I'm somehow passing through the rules twice. I am also slightly bemused as to where the home bit of the URL comes from in the first example.

Can someone point me in the right direction, please?

Thanks, J.

© Server Fault or respective owner

Related posts about apache2

Related posts about webserver