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.

Refresh the tab to see the changes.

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

FAQ
Q
How do I generate an .htpasswd file without having Apache tools installed?
A
In Linux (and other Posix): given users John, Mary, Jane, and Jim with passwords V3Ry, SEcRe7, V3RySEcRe7, and SEcRe7PwD, in order to generate a password file named .htpasswd, you would issue:
Q
is possible to configure HAProxy Load Balancer in Nginx?
A
follow this link : "https://www.linuxhelp.com/how-to-configure-load-balancer-with-haproxy-for-nginx-in-centos/ "
Q
how to install HAPROXY ?
A
follow this link : "https://www.linuxhelp.com/install-haproxy-load-balancer-ubuntu/"
Q
is same procedure for centos nginx load balancer?
A
follow this link : " https://www.linuxhelp.com/how-to-configure-nginx-as-a-load-balancer-in-centos/ "
Q
is any one know how to install CROSSROADS Load Balancer?
A
follow this Link : " https://www.linuxhelp.com/set-xr-crossroads-load-balancer-linux/ "