How to Configure Nginx Load Balancer in Ubuntu

To Configure Nginx Load Balancer in Ubuntu

Nginx is an open source and high performance web server for Linux distributions. It acts as a reverse proxy server and Load Balancer in order to distribute incoming traffic around several virtual private servers. In this article let’s see how to configure Nginx as a load balancer in Ubuntu.

 


Configuration of Nginx Load Balancer

Prerequesites:

Setup three servers with any IP and Host name as shown below,

Load Balancer Server

Hostname : nginxbalancer.com
Ip Address : 192.168.5.123

Web-server 1

Hostname : webserver1.com
IP address : 192.168.5.146

Web-server 2

Hostname : webserver2.com
IP address : 192.168.5.155


To install Nginx

Utilize the following command to install the Nginx package.

root@ngnixload:/home/user1# apt-get install nginx
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
nginx-common nginx-core
.
.
.
Setting up nginx (1.10.0-0ubuntu0.16.04.4) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for systemd (229-4ubuntu4) ...
Processing triggers for ufw (0.35-0ubuntu2) ...

Then configure the installed package.

root@ngnixload:/home/user1# vim /etc/nginx/sites-available/default
upstream backend {
server 192.168.5.146;
server 192.168.5.155;
}

server {
listen 80;

location / {
proxy_pass http://backend;
}
}

 

 

Once the Nginx is configured, open the web-root directory to create a file and add some text in the web server1.

Repeat the above step for webserver2.

root@webserver1:/var/www/html# vim index.html

Lets have a look of a Nginx Load balancer  in web browser with Load balancer IP.

Nginx-web-server-Load-balancerIP

Refresh the tab to see the changes.

Nginx-web-server-Load-balancer-Refresh

Run the following command to check the output in command prompt.

root@ngnixload:/home/user1# curl localhost
server1
root@ngnixload:/home/user1# curl localhost
server2

 

 


Thank you! for using Linux Help.

You find this tutorial helpful? Share with your friends to keep it alive.
For more help topics browse our website www.linuxhelp.com
Be the first to comment, we value your suggestions. For further queries please comment below.

Tags: Nginx Load balancing Web Server Load balancer Reverse-proxy
user image Author :  Nicholas