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.
apt-get update
Let’s install Nginx now.
apt-get install nginx
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.
Note: I have edited the index.html using the below command and added a text to identify the node uniquely.
vim /usr/share/nginx/html/index.html
Repeat the same on node 02 as well.
Step 02: Install Keepalived
In both nodes, run the following command to get Keepalived installed.
apt-get install keepalived
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.
vim /etc/keepalived/keepalived.conf
Now let’s paste the script below. Don’t forget to replace the FLOATING_IP with your floating IP.
global_defs { # Keepalived process identifier lvs_id nginx_DH } # Script used to check if HAProxy is running vrrp_script check_nginx { script "killall -0 nginx" interval 2 weight 2 } # Virtual interface # The priority specifies the order in which the assigned interface to take over in a failover vrrp_instance VI_01 { state MASTER interface eth0 virtual_router_id 51 priority 101 # The virtual ip address shared between the two loadbalancers virtual_ipaddress { FLOATING_IP } track_script { check_nginx
Now open the keepalived.conf file using the below command in the node 02.
vim /etc/keepalived/keepalived.conf
Like earlier, now paste the script below. Don’t forget to replace the FLOATING_IP with your floating IP.
global_defs { # Keepalived process identifier lvs_id nginx_DH } # Script used to check if HAProxy is running vrrp_script check_nginx { script "killall -0 nginx" interval 2 weight 2 } # Virtual interface # The priority specifies the order in which the assigned interface to take over in a failover vrrp_instance VI_01 { state SLAVE interface eth0 virtual_router_id 51 priority 101 # The virtual ip address shared between the two loadbalancers virtual_ipaddress { FLOATING_IP } track_script { check_nginx } }
Let’s restart Keepalived in both nodes using the following command.
service keepalived restart
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.
vim /etc/nginx/sites-enabled/default
Under location in the config file, you need to put your backend server IP (here, it is 192.168.XXX.YYY).
proxy_pass http://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.
upstream backend_servers { least_conn; server 192.168.XXX.YYY; server 192.168.XXX.YYZ; server 192.168.XXX.YZZ; } server { listen 80; server_name dasunhegoda.com; location / { proxy_pass http://backend_servers; } }
You can configure the same on node 02 to have your reserve proxy in high availability.
Summary
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).
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.