Configuring HAProxy for use with Apache Tomcat
Problem
At $WORK we’ve been evaluating options other than Apache HTTPD as a front-end, loadbalancer, and session manager for Apache Tomcat due to unreasonably high load on the Apache servers, and we came across HAProxy as an option. Here’s how we ended up configuring HAProxy for Apache Tomcat with sticky sessions! It seems that few sites were posting exactly how to do this (or maybe I just suck at searching!), so I thought I should.
Installing HAProxy
Luckily, HAProxy is a well-known and mature product and thus should be in your favorite distribution’s package repository.
To install on Debian and derivatives:
apt-get install haproxy
To install on Redhat and derivatives:
yum install haproxy
Configuring HAProxy
For the impatient, here’s a lightly edited version of /etc/haproxy/haproxy.cfg with brief annotations:
# haproxy.cfg global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 frontend test1 mode http bind *:80 default_backend testback backend testback mode http stats enable stats auth username:password stats uri /haproxy option redispatch option forwardfor option httpchk OPTIONS /qds-portal balance url_param JSESSIONID check_post cookie JSESSIONID prefix server tc48 host48.example.com:8080 check cookie tomcat48 # tomcat48 = jvmRoute in tomcat/conf/server.xml server tc49 host49.example.com:8080 check cookie tomcat49 # tomcat49 = jvmRoute in tomcat/conf/server.xml
Thank you very much 🙂