Using Apache As Proxy For Java Applications

Just a short how-to enable Apache web server to serve as a proxy for Java applications. I assume a Java application server bound to localhost:8080. The commands shown relate to a Debian GNU/Linux. I show how to proxy a request to / into a Java web application in an application server on a different host, port and with a context other than /, so how to is served from http://localhost:8080/example/.

At first, enable the needed modules proxy, proxy_http, proxy_ajp and rewrite:

a2enmod proxy
a2enmod proxy_http
a2enmod proxy_ajp
a2enmod rewrite

You can also enable all modules with one command line:

a2enmod proxy proxy_http proxy_ajp rewrite

This will result in the following entries in /etc/apache2/httpd.conf, e.g.:

LoadModule proxy_module      libexec/apache22/
LoadModule proxy_http_module libexec/apache22/
LoadModule proxy_ajp_module  libexec/apache22/
LoadModule rewrite_module    libexec/apache22/

or in links from /etc/apache2/mods-enabled into /etc/apache2/mods-available:

ls -l /etc/apache2/mods-enabled/
total 0
lrwxrwxrwx 1 root root 28 Jan 12 2012 proxy.conf -> ../mods-available/proxy.conf
lrwxrwxrwx 1 root root 28 Jan 12 2012 proxy.load -> ../mods-available/proxy.load
lrwxrwxrwx 1 root root 28 Jan 12 2012 proxy_ajp.load -> ../mods-available/proxy_ajp.load
lrwxrwxrwx 1 root root 28 Jan 12 2012 proxy_http.load -> ../mods-available/proxy_http.load
lrwxrwxrwx 1 root root 28 Jan 12 2012 rewrite.load -> ../mods-available/rewrite.load

Create a virtual host configuration file under /etc/apache2/sites-available/

AJP Proxy

You should use AJP to proxy connections to the application server.

One reason is a simple example: When Apache is terminating SSL and the application server is accessed using a HTTP proxy (see below), the Java application does not know about this and (in case of a web application) may produce links without https:// prefix.

<VirtualHost *:80>
    <IfModule mod_proxy.c>
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia On
        <IfModule mod_proxy_ajp.c>
            ProxyPass /context ajp://

HTTP Proxy

You can use mod_proxy_http to configure a HTTP proxy. When needed, you can also use mod_rewrite with the [P] flag.


<VirtualHost *:80>
    <IfModule mod_proxy.c>
        # Disable proxying remote requests, we want a reverse proxy
        ProxyRequests Off
        ProxyPreserveHost On
        ProxyVia On
        <Proxy *>
            Order deny,allow
            Allow from all
        # Proxy requests to / into the correct context on localhost:8080
        ProxyPass /


<VirtualHost *:80>
    <IfModule mod_rewrite.c>
        # Enable rewrite module
        RewriteEngine On
        # Rewrite URLs containing the context
        RewriteRule ^/example$ /$1/ [L]
        RewriteRule /example/(.*) /$1
        # Use Java application server on localhost:8080
        RewriteRule /(.*)$1 [P]

… and enable the new site:


Enabling site Run ‘/etc/init.d/apache2 reload’ to activate new configuration!

After restarting Apache

service apache2 restart
Restarting web server: apache2 ... waiting ..

you should be able to access your Java web application through



This entry was posted in Software Development, System Administration and tagged , . Bookmark the permalink.