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 :

Nginx load balancer
Nginx load balancer

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 ufw upon installation. You can check all the application that ufw knows by typing :

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/nginx/sites-available/default . If you dont want to run your server in default port 80, either change it in /etc/nginx/sites-available/default or disable it from nginx.conf.

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 least number of existing active connection and forwards the incoming request.

# 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;
}
}

Thats it!. Restart nginx and you should now be able to send a request to http://localhost and the request will be served by one of the Tomcat servers.

mm

Anup Chhetri

IT system administrator

You may also like...

error: Content is protected !!