Nginx is one of the most famous web servers in the today’s market which provides a set of features that the Enterprise can utilize to scale out the infrastructure. Today, we are going to focus on installing Nginx on Ubuntu, setting it up as a reverse proxy and to load balance (high availability) with Keepalived. I have done a similar article for HAProxy some time back (if you are interested check this link). Without further ado let’s jump into the installation of Nginx. In order to complete the following tutorial, you will need two servers/nodes and a floating IP/virtual IP. Further, if you don’t intend to set up a HA cluster you can direct yourself to part 02 of the tutorial.

Part 01 : Load Balancing

Step 01: Install Nginx

SSH to the node 01 and get root access or go with sudo. The below command will get an update of the packages. Read more here.

Let’s install Nginx now.

Now you have a Nginx server up and running. Go to the browser and type your IP and see. You should get the below output.

nginx node 01

Note: I have edited the index.html using the below command and added a text to identify the node uniquely.

Repeat the same on node 02 as well.

Step 02: Install Keepalived

In both nodes, run the following command to get Keepalived installed.

For this tutorial, let’s assume node 01 is the master and node 02 is the slave.

Open the keepalived.conf file using the below command in the node 01.

Now let’s paste the script below. Don’t forget to replace the FLOATING_IP with your floating IP.

Now open the keepalived.conf file using the below command in the node 02.

Like earlier, now paste the script below. Don’t forget to replace the FLOATING_IP with your floating IP.

Let’s restart Keepalived in both nodes using the following command.

Finally! Now you should be able to see that all of your requests are routing to the master. If you take down the master, all of your requests will go to slave. Congratulations!

Part 02 : Reverse Proxying

Nginx as a Reverse Proxy

In order to get started, you need to first install Nginx using the step 01 in part 01 above.

Now let’s route the traffic to our backend servers. Here, Nginx will perform as a reserve proxy. For the sake of simplicity lets edit the default vhost configuration file as below.

Under location in the config file, you need to put your backend server IP (here, it is 192.168.XXX.YYY).

There you go. Now all of your requests should be routed to your backend server.

Nginx as a Reverse Proxy with Load Balancing

As earlier, you can edit the default vhost configuration file as below.

You can configure the same on node 02 to have your reserve proxy in high availability.


Nginx is a great product and you can do a lot with it. The purpose of this article is to give you a sneak peek on what it can do. You can explore the beauty by yourself now. That’s about it! If you have any questions, let me know in the comments below. Don’t forget to give your feedback on the post (happy-face).

1 Comment

  1. Christophe HU February 12, 2018 at 9:46 pm

    Thanks for sharing.
    Is the FLOATING_IP 192.168.XXX.YYY In your example ?
    Is nodeO1 with IP 192.168.XXX.YYZ and nodeO2 with 192.168.XXX.YZZ ?
    When you config the nginx load balancing I saw 3 IPs but you only have 2 nodes.


Leave A Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.