Reading Time: 5 minutes

Nginx (pronounced ‘engine x') is an HTTP and reverse proxy server. It is well known for its high performance and stability. It is pretty feature rich and very simple to configure. Nginx hosts nearly 12.18% (22.2M) of active sites across all domains. Nginx uses architecture to handle requests. When compared to a thread-per-request model, -driven is highly scalable with a low and predicatble memory footprint.

Nginx is very easy to setup as a load balancer for an Apache Tomcat farm. In this blog post, I will show you how to set it up as a round-robin load balancer for two Apache Tomcat servers.

latest report
Learn why we are the Leaders in management and

You first need to install nginx, you can find the installation instructions here. If you are on a Mac, then you could use Homebrew
brew install nginx
After installing, you can run it using the nginx command
sudo nginx
You can now test the installation by opening the following URL in a browser
http://localhost:8080
Lets change the default port 8080 to port 80. You can do that in the nginx.conf file which by default is located at /usr/local/etc/nginx/nginx.conf. First stop nginx
sudo nginx -s stop
Now open the nginx.conf file and locate the listen attribute of the server. Change the value from 8080 to 80. Here is where I made the change in my configuration:

<br>
server {<br>
listen 80;<br>
server_name localhost;<br>

Save the file and start nginx. You should now be able to test the configuration change by visiting http://localhost
Install two instances of Apache Tomcat. Open their server.xml files and change the HTTP port numbers to 8080 and 8081 respectively. You can find more information on Apache Tomcat configuration here. Once you have made the change and saved the configuration files, you should now start each instance of Apache Tomcat. Here are a few ways you can start Apache Tomcat. Typically you should be able to locate the bin directory of inside your Tomcat installation and invoke the startup.sh file as shown:
bin/startup.sh

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 nginx.conf, locate the http block and add the upstream block to create a group of servers:

<br>
http {<br>
upstream tomcat_servers{<br>
ip_hash;<br>
server 127.0.0.1:8080;<br>
server 127.0.0.1:8081;<br>
}<br>

ip_hash above 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.

Finally, you need to locate the location block within the server block and map the root(/) location to the tomcat_servers group created using the upstream module above.

<br>
location / {<br>
proxy_pass http://tomcat_servers;<br>
}<br>

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.