Goodbye mod_jk hello mod_proxy
Summary
I’ve been using Apache as a front end to a cluster of Tomcat servers for a while. I’ve always used mod_jk2 or mod_jk; I even wrote an article about it. Not any more.
The most recent Apache comes with mod_proxy, mod_proxy_balancer and mod_proxy_ajp, which do everything I used mod_jk to do.
Setup mod_proxy_balancer
First you need to make sure they are included when you compile Apache:
./configure –prefix=/usr/local/apache2.2 –enable-proxy –enable-proxy-ajp –enable-proxy-balancer –enable-proxy-http
Then in your httpd.conf you need: <proxy balancer://myCluster> BalancerMember ajp://bill.darkcoding.net:8101 route=server1 BalancerMember ajp://ben.darkcoding.net:8102 route=server2
<location /myApp>
ProxyPass balancer://myCluster/myApp stickysession=JSESSIONID
</location>
<location /balancer-manager>
SetHandler balancer-manager
AuthType Basic
AuthName "Cluster manager "
AuthUserFile /usr/local/apache2.2/conf/.htpasswd
Require valid-user
</location>
The above defines a cluster and adds your two servers to it. Then it proxies your app location using that cluster. Finally it defines a URL for the balancer manager. This is the replacement for jkmanager. The stickysession value needs to be the name of the session url param name or cookie. The above is correct for Tomcat. Note that the session id is case sensitive.
Finally in you Tomcat server.xml you need to include a jvmRoute to match the route defined above. This is exactly the same as when using mod_jk, so you probably already have it. It should look somewhat like this:
<engine name="Catalina" defaultHost="localhost" debug="0" jvmRoute="server1">
and similarly for server2.
Eh voila !
If it’s not working change your LogLevel in httpd.conf to debug and restart Apache. If you get this error:
Found value (null) for stickysession JSESSIONID
then Apache is not finding your route on the session id. Check the spelling and capitalisation of the stickysession entry. Using the Firefox developer toolbar to check you Cookie. It should look something like JSESSIONID=4f39cBLAH.server1. The route is appended to the session id after a period.
There is also apparently a problem with disabled servers coming back to life. See this thread
Happy proxying !