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 http://www.example.com/ 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/mod_proxy.so
LoadModule proxy_http_module libexec/apache22/mod_proxy_http.so
LoadModule proxy_ajp_module  libexec/apache22/mod_proxy_ajp.so
LoadModule rewrite_module    libexec/apache22/mod_rewrite.so

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/www.example.com:

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://127.0.0.1:8009/context
        </IfModule>
    </IfModule>
</VirtualHost>

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.

mod_proxy

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin webmaster@example.com
    <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>
        # Proxy requests to / into the correct context on localhost:8080
        ProxyPass / http://127.0.0.1:8080/example/
    </IfModule>
</VirtualHost>

mod_rewrite

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin webmaster@example.com
    <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 /(.*) http://127.0.0.1:8080/example/$1 [P]
    </IfModule>
</VirtualHost>

… and enable the new site:

a2ensite www.example.com

Enabling site www.example.com. 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 http://www.example.com/.

HTH.

Resources

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