Tomcat load balancing with Apache using mod proxy

This tutorial will show you how to configure Tomcat Load Balancing with Apache webserver (http server) using Mod Proxy. I have listed here the following steps on how to configure Apache with Tomcat to configure Load Balancer using Mod Proxy.

Having load-balanced is always recommended in production environment for better availability.

Load balancing improves responsiveness and increases availability of applications. A load balancer sits between the client and the server farm accepting incoming network and application traffic and distributing the traffic across multiple backend servers using various methods.

JRE is installed and already configured

Please follow the below steps for configuring Tomcat load balancing with Apache Tomcat and Apache http servers.

Step 1. Download Apache 2.4 http server from

Step 2. Download Apache Tomcat Server version 8 from

Step 3. Install Apache http server, let’s say in D drive. Here I used xampp to install the Apache 2.4 http server. The root folder of the server is D:\xampp\apache.

Step 4. Install two instances of Apache Tomcat server, let’s say in C drive. Here I have used version apache-tomcat-8.0.36. So I have put two instances of apache-tomcat-8.0.36 server – C:\apache-tomcat-8.0.36 and C:\ apache-tomcat-8.0.36-2.

Step 5. Change the ports in Tomcat server in server.xml file. Here we will use default ports in C:\apache-tomcat-8.0.36 and change ports in C:\ apache-tomcat-8.0.36-2. Now open the file C:\apache-tomcat-8.0.36\conf\server.xml and update as following:

<Server port="8015" shutdown="SHUTDOWN">
<Connector port="8090" protocol="HTTP/1.1"
               redirectPort="8443" />
<Connector port="8019" protocol="AJP/1.3" redirectPort="8443" />

Step 6. Add/Uncomment below lines into httpd.conf file in Apache http server:

LoadModule proxy_module modules/
LoadModule proxy_balancer_module modules/
LoadModule proxy_http_module modules/
LoadModule slotmem_shm_module modules/
LoadModule lbmethod_byrequests_module modules/

Step 7. Add below load balancing configuration into conf file httpd.conf in Apache http server. Add proxy pass along with balancer name for application context root, for example, here it is spring-rest.

In this example, I have proxy path as spring and balancer name as mycluster. It is important to include stickysession as not having this option will distribute same request to multiple tomcat servers and you will have session expiry issues in application.

Reverse Proxy

Reverse proxy is to provide Internet users access to a server that is behind a firewall. Reverse proxy is helpful when you want to access your internal application from internet.

The User does not know about your internal server, and looks to them that the response is coming from the proxy server itself.

Sticky Session

As we know load balancer send the requests to the back-end servers as a round robin manner. We will have to use the sticky session feature which enables the load balancer to stick a user’s session to a specific back-end server. This would make sure all requests coming from the user during the session will be sent to the same server.

#Load Balancer Configuration - start
<IfModule proxy_module>
    <Location "/balancer-manager">
        SetHandler balancer-manager
        Require all granted
	ProxyRequests Off
	ProxyPass /spring balancer://mycluster stickysession=JSESSIONID
	ProxyPassReverse /spring balancer://mycluster stickysession=JSESSIONID
	<Proxy balancer://mycluster>
		BalancerMember http://localhost:8080/spring-rest route=server1
		BalancerMember http://localhost:8090/spring-rest route=server2
#Load Balancer Configuration – end

As you can see in above configuration, I have added route in BalancerMember so route value can be added to jvmRoute parameter in server.xml of Tomcat.

Step 8. Now update the server.xml files in each instance of Tomcat server. Add jvmRoute parameter in server.xml of Tomcat. This must be added in Engine name tag.

<Engine name="Catalina"  defaultHost="localhost" jvmRoute="server1">
<Engine name="Catalina"  defaultHost="localhost" jvmRoute="server2">

Step 9. Upadte the Logformat in httpd.conf to see which balancer was selected appended with the request URIs in log file. Add following in LogFormat directive under <IfModule log_config_module/> in httpd.conf

The complete line looks like below

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"% %{BALANCER_WORKER_ROUTE}e\"" combined

Step 10. Now put the each instance of spring-rest.war files under C:\apache-tomcat-8.0.36\webapps and C:\apache-tomcat-8.0.36-2\webapps. The war can be generated from the application posted here

Step 11. Now start each instance of the Tomcat server, for example, by executing startup.bat file from the cmd prompt “C:\apache-tomcat-8.0.36-2\bin>startup.bat”

Step 12. Now start the http server(here I have used to install xampp, so I can start using xampp Control Panel) and access the URL http://localhost/balancer-member in browser. From the below figure you would be able to understand the specifications of tomcat load balancing with apache.

tomcat load balancing with apache

Step 13. Now access application URL by balancer path http://localhost/spring/. You can see output in the browser – “Hello World”. When you access URL http://localhost/spring/hello/Soumitra, then you will see output – “Hi Soumitra, Good Morning!”


Check access log of apache server to ensure your request is getting routed to only one tomcat instance. You will also notice your route id is appended as shown in the below example:

::1 - - [Date Time] "GET /spring/hello/Soumitra HTTP/1.1" 200 26 "-" "Mozilla/5.0 (Windows NT 6.1; rv:56.0) Gecko/20100101 Firefox/56.0" "server1"
::1 - - [Date Time] "GET /spring/hello/Soumitra HTTP/1.1" 200 26 "-" "Mozilla/5.0 (Windows NT 6.1; rv:56.0) Gecko/20100101 Firefox/56.0" "server2"

That’s all. Hope you have understood how to configure tomcat load balancing with apache server.

Thanks for reading.

Related posts

Leave a Comment