Configure NGINX (pronounced “engine X”) as a load balancer for Apache Tomcat
With multiple options for load balancing solution, Apache HTTPD, HAProxy & Nginx are currently the popular ones. Nginx is a popular open-source web server. This can be used as Load balancer as well as a reverse proxy. In this section, we will discuss Nginx as a load balancer. For reverse proxy configuration, you can visit my another post.
As a prerequisite, you’ll need to have at least two hosts with an Apache Tomcat servers installed and configured to see the benefit of the load balancer.
The scenario looks somehow like this :

Lets start with NGINX installation. For Linux environment, you can use apt install tool.
sudo apt install nginx
If you have firewall software, please adjust to allow access to the service. Nginx by default adds itself to
sudo ufw app list
To enable respective application and verify the status run following command:
sudo ufw allow 'Nginx HTTPS' // allow HTTPS ( port 443)
sudo ufw status
If you have other ports, do not forget to configure them in firewall.
You can test the installation by opening the following URL in a browser:
http://localhost:80
If you want to change to other ports, you can modify nginx.conf located in /etc/nginx/nginx.conf
By default the configuration will be taken from /etc/
For adding any new configuration, create a configuration file under /etc/nginx/conf.d . This configuration file will be added automatically in NGINX , once the service is restarted.
I assume that two Apache servers are running in two different machines. For the test, you can run two Apache server instances in a single machine. But don't forget to change the default port (8080 ) to something else in a second server. Open their server.xml files and change the HTTP port numbers.
But for now, we will use two machines running two instances of Apache Tomcat server. That means, both instances can run in default port 8080, since IP is different for both machines.
Setup Round Robin Nginx Load Balancer
Now that the Tomcat instances are started, lets setup the round-robin load balancer. You would need to use the nginx upstream module. The upstream module allows you to group servers that can be referenced by the proxy_pass directive. In your conf file (/etc/nginx/conf.d/) , add the upstream block to create a group of servers:
# cat /etc/nginx/conf.d/mylocal-lab.local.conf
upstream MyCluster1 {
server TOMCAT1_IP_ADDRESS:8080;
server TOMCAT2_IP_ADDRESS:8080;
}
server {
listen 80;
server_name mylocal-lab.local www.mylocal-lab.local;
location / {
proxy_pass http://MyCluster1;
}
}
Setup least-connected Nginx Load Balancer
In this example, NGINX will use Least-connected method for load balancing. This algorithm checks to the server that has
# cat /etc/nginx/conf.d/mylocal-lab.local.conf
upstream MyCluster2 {
server TOMCAT1_IP_ADDRESS:8080;
server TOMCAT2_IP_ADDRESS:8080;
least_conn; # keyword telling nginx to use least connection
}
server {
listen 80;
server_name mylocal-lab.local www.mylocal-lab.local;
location / {
proxy_pass http://MyCluster2;
}
}
Setup Persistence or Sticky Nginx Load Balancer
IP_hash configures the load balancing method where requests are routed to servers based on IP Addresses of the client i.e. a request from a client with a particular IP will always go to the same backend Tomcat Server. This is particularly useful when Tomcat application requires basic session persistence ( sticky sessions ).
# cat /etc/nginx/conf.d/mylocal-lab.local.conf
upstream MyCluster3 {
server TOMCAT1_IP_ADDRESS:8080;
server TOMCAT2_IP_ADDRESS:8080;
ip_hash; # keyword telling nginx to configure session persistence
}
server {
listen 80;
server_name mylocal-lab.local www.mylocal-lab.local;
location / {
proxy_pass http://MyCluster3;
}
}