Version 2

    Load Balancing using mod_rewrite and mod_proxy:

     

    This is only useful if you can't upgrade Apache httpd to 2.2.x. (See Using mod_proxy with JBoss/Tomcat) and you have to use SSL between Apache httpd and Tomcat.

     

    Well mod_jk is obviously a more elegant solution, but you CAN do this with mod_proxy when used with mod_rewrite.

     

    Load balancing isn't quite as straight forward with mod_proxy as a simple server.  You'll need to configure both mod_proxy and mod_rewrite.  Fortunately, the JBoss/Tomcat side is simple to configure.

     

    The JBoss/Tomcat side

     

    Edit $JBOSS_HOME/server/$config/deploy/jbossweb-tomcat-41.sar/META-INF/jboss-service.xml find the Engine element and add an attribute called jvmRoute.  Set its value to LB1.

     

    <Engine name="MainEngine" defaultHost="localhost" jvmRoute="LB1">
    ...
    

     

     

    Do the same for each server in the group, giving each a different ID: LB2, LB3, ...

     

    The Apache side

     

    First we must create a rewrite map (for mod_rewrite) (balancing.conf):

     

    LB1  backend1.jboss.org:8080
    LB2  backend2.jboss.org:8080
    LB3  backend2.jboss.org:8888
    ALL  backend1.jboss.org:8080|backend2.jboss.org: 8080|backend2.jboss.org:8888
    

     

    This means each worker is associated with a specific server and port.

     

     

    In httpd.conf:

     

    RewriteMap SERVERS rnd:/usr/local/httpd/conf/servers.conf
    
    <Location /webapp>
        RewriteEngine On
    
        RewriteCond "%{HTTP_COOKIE}"                  "(^|;\s*)jsessionid=\w*\.(\w+)($|;)"
        RewriteRule "(.*)"                            "http://${SERVERS:%2}%{REQUEST_URI}"  [P,L]
        RewriteRule "^.*;jsessionid=\w*\.(\w+)($|;)"  "http://${SERVERS:$1}%{REQUEST_URI}"  [P,L]
        RewriteRule "(.*)"                            "http://${SERVERS:ALL}%{REQUEST_URI}" [P,L]
    </Location>
    

     

    The first entry means that we want to load the server map which we created.  The "ALL" entry in the server map will affect those users who do not yet have a session ID.  Meaning the session cookie which tomcat creates will have the LBx id in it, the other entries will govern which host we're directed to.

     

    The rewrite rules govern those users with a cookie.  Apache will rewrite the URL and point you to one of the instances of Tomcat.  This is vodoo but it should work.

     

    Thanks to Pier Fumagalli for helping document this method.