NGINX as Load Balancer

NGINX (one of the worlds fastest web servers) can be used as a load balancer too. It started out as a web server designed for maximum performance and stability. In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers. In this post we will see the workings of NGINX as Load balancer for Mule servers as well as Apache servers (or in general any two HTTP URLs).
If  you want to try out with Mule, head to configuring Mule Servers Section, If You want to try without Mule Server, you can head to configuring  Apache Servers Section

Configuring Mule Servers:

In case you already have Mule servers configured/working, skip to NGINX configuration section. If you don’t have mule server configured, download it from

You need to provide your details to start the download.

Unzip the folder

Then, go to bin folder

Now, when you run mule.bat file  your mule server starts running.

Now,  we will run simple application on this server which return a simple json output (without any header information) to test from a browser. I am creating my application with name app1 having path  “/api/keepalive” listening at localhost:8081 and payload as “{message:success from mule sever1}”

If you want complete source code, you can get it from here

Configuration xml of application app1 is like below..


Now, we want zip of this project to run in Mule server. One way to get the zip file is from anypoint studio.  If you are using anypoint studio to build your application.. Right click on the project and go to Export > Mule > Anypoint Studio Project to Mule Deployable Archive

When you place this zip into your apps folder, the server starts deploying.

When you place your  in Mule Server’s apps folder, it will create “app1” folder and app1-anchor.txt.

Let’s try hitting URL localhost:8081/api/keepalive, You will see the result as below

Create second Mule server:

In order to check load balancing using nginx, we need to add one more Mule server. So, You can again download the server to new location or add copy of this same standalone server in another location. Then, open bin folder and run mule.bat again. Your second server will also starts running. Then,  add another simple application like above that has same path “api/keepalive” listening at  localhost:8082 and payload as “{message:success from mule sever2}”

If you want complete source code, you can get it from here

Configuration xml of application app2 is like below..

Note that we changed the payload just to differentiate which server is responding back to the caller.

Then, make a zip of this project and place it in apps folder of second server. It will start deploying

Let’s hit the URL localhost:8082/api/keepalive

Now, your two mule servers are configured successfully. Next, For testing how NGINX server do load balancing go to NGINX configuration section.

Configuring Apache Servers:

Install and configure simple Apache Web Servers in Windows


Download from Apache Lounge

Unzip the folder to somewhere in your drive. In my case, I have unzipped it in C:\ProgramSoftwares\Apache24 folder

Unfortunately you won’t be able to run just yet- because the default downloader assumes that the directory is directly under your drive letter (for eg: C:\Apache24 or D:\Apache24….). If you have unzipped in these locations, you can skip to starting the apache server directly – else, please follow the below instructions.

In Apache24, go to config folder, open httpd.conf file and modify ServerRoot and DocumentRoot to your path and change port to 8081

Run the httpd server:

If you run the httpd server, and hit the port your apache is listening on, you will see the page like below:

For testing out the load balancer operation, let’s add a bit more to this. Let’s configure a mocked URL (/api/keepalive) which will return a simple json output (without any header information) to test from a browser. To do this, in the htdocs folder, add a folder called api and add a file called keepalive under it.

Keep the entry in keepalive, similar to as shown below:

If you hit the apache URL, you should be able to see the response as shown below:

Create second Apache web server:

In order to test the load balancing using nginx server, let’s create a second apache server to listen on 8082 with a similar /api/keepalive and payload as {message:success from Apache server2}. You can re-create all the steps of installation like above or copy the apache24 directory again to another folder and change the configuration.

End result we want to see is the response when we hit the second server should look like below:

Note that keepalive file contents are changed just to differentiate which server is responding back to the caller.

Now, we have configured Apache servers successfully. Now go to Configuring NGINX Server to check the load balance operation

Configuring NGINX Server:

Download nginx server from website

Unzip the nginx to a folder on your box:

Configure NGINX listen port:

Start nginx server

Note that when you bring up nginx, two processes with nginx will be running in Windows – which is normal.

Access NGINX Server through browser

Add a simple rule:

We want nginx to act as load balancer – so that when we hit ://<nginxServer>/api/…. – it should load balance between the servers configured as cluster nodes. For our example, we are going to load balance between servers running on 8081 and 8082

To do this, add upstream construct to nginx configuration inside http construct

Add a location matching rule:

Note that the rule does a pattern matching of all URLs which begin with /api/… in a case insensitive way. Once it finds a URL matching the location, it will forward the request to one of the load balanced URLs (in our case 8081 and 8082)

Reload the nginx configuration to pick up the changes

For Mule Servers configuration, result will be like below

If you refresh the browser a couple of times, you will see a message from second server as well – which is load balanced…

For Apache servers also we will get result similarly like below..

If you want to add sticky session (basically lock the server to a client), you can add ip_hash directive to the upstream directive

After making the changes, reload nginx configuration (using nginx.exe -s reload command) and then try out the URL from browser. No matter how many times you refresh the URL, you will keep getting the response from the same server.

Couple of other tests:

Hit an invalid URL – which apache server/mule server can’t handle like keepalive2 as shown below:

Note that this “Not Found” is returned from Apache Web server and not from NGINX.

To test out how NGINX returns not found, hit a invalid URL which NGINX can’t handle like /api2/keepalive

Debug steps:
In case you want to debug the NGINX rules a bit further, enable logging and access logs as shown below

To stop nginx server, issue below command:



You can download softwares required from below links

configuration xml of app1 and app2 can be availbale from below links!api/2.0/snippets/nafeesa/EbjrL/6b63787d3e2b067b4f3619580a3e578b3172a559/files/app1!api/2.0/snippets/nafeesa/EbjrL/6b63787d3e2b067b4f3619580a3e578b3172a559/files/app2.xml

complete source code of app1 and app2 can be available from below links

Leave a Reply

Your email address will not be published. Required fields are marked *


sixteen + 15 =