Apache and JBoss integration with mod_rewrite and mod_proxy

By Stephane Carrez 1 comments

To integrate JBoss behind an Apache server you often use the mod_jk module. There is another solution which combines the use of Apache mod_rewrite and [

The problem I wanted to solve was to be able to use the Apache URL rewrite before forwarding the request to a JBoss server. The Apache and JBoss were already integrated with mod_jk (Deploying a J2EE application behind an Apache server). The URL rewriting rule does not work in that case (at least I was not able to make it work). I investigated the Apache mod_rewrite and its proxy configuration.

First, in your Apache host configuration you have to enable the Apache rewrite module. This is done by the RewriteEngine directive. To make sure that the server name is propagated to JBoss, you have to use the ProxyPreserveHost directive. If you don't do this, JBoss will receive 'localhost' as server name (ie, the servlet request getServerName() method will not return what you expect). You then define your rewrite rule and use the proxy forwarding mode indicated by [P].

 <VirtualHost *:80>
    RewriteEngine On
    ProxyPreserveHost On
    RewriteRule ^/some-path/(.*)$  \
           http://localhost:8080/web-app-deploy-name/$1 \[P\]
   ...
  </VirtualHost>

You have to make sure the Apache modules are available, for this execute these commands:

  sudo a2enmod proxy proxy_http rewrite

Once your configuration files are ready, reload Apache configuration:

  sudo /etc/init.d/apache2 reload

That it!

This mod_rewrite and mod_proxy configuration is very powerful and easier to setup than mod_jk.

Add a comment

To add a comment, you must be connected. Login

1 comments

gcorrigan@harrisonpub.com
GTC on 2010-03-05 16:38:29 said:

Good tip. Would suggest you change the Rewrite to include the trailing / in the RegEx parameter... then this URL will also work:
http://yourserver/some-path
and run the default page from JBoss.

RewriteRule ^/some-path(.*)$ \
http://localhost:8080/web-app-deplo... [P]