How to configure NGINX load balancer on CentOS 7 linux step by step guide



In this tutorial we learn how configure NGINX load balancer on Rhel/CentOs/Oracle (7/8)linux system. NGINX acts as a reverse proxy as well as load balancer also to distribute traffic among several backend servers to manage the serve load.

Lab environment

Load balancer server ipaddress  ================>192.168.137.2
Hostname of proxy server==============> lb.example.com

We have two web servers .
web server1 ip address ===============>192.168.137.3
hostname of webserver1 ==============>web1.example.com

web server2 ip address ===============>192.168.137.4
hostname of webserver2 ==============>web2.example.com

and put the host file entry if you don’t have any dns server like this.
[root@lb]# cat /etc/hosts
192.168.137.2     lb.example.com
192.168.137.3     web1.example.com
192.168.137.4     web2.example.com

 

Configuration of webservers


Before configuration of NGINX load balancer server first we have to configure a sample web server to demonstrate. For this we use apache web server. First we install httpd package and configure it and create a sample index.html file . to do this follow the below step.

To install apache web server we execute the below command.

#yum install httpd

After installing the httpd server start the service and on the startup mode using the below command.

#service httpd start;chkconfig httpd on

Now create a sample index.html file for webserver1 execute the below command

#echo “welcome to server1” > /var/www/html/index.html

Now create a sample index.html file for webserver2 execute the below command

#echo “welcome to server2” > /var/www/html/index.html

Configuration of NGINX load balancer on linux

 

To configure NGINX as a load balancer we need to install NGINX on your linux machine  .
See the reference to install the NGINX on linux system



After installing the NGINX we go to the configuration file of the NGINX server which is located at “/etc/nginx/conf.d/default.conf”  .

we remove the default configuration file using the below command.

#rm /etc/nginx/conf.d/default.conf 

and create a new file named "loadbalancer.conf".

add the below lines into the loadbalancer.conf file.
upstream backend {
   server 192.168.137.3;
   server 192.168.137.4;
}


server {
   listen 80;

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

you can check the configuration syntax it is correct or not using the below command.

#nginx -t

Verify the configuration of NGINX load balancer


After completed the configuration verify the configuration that it is configured successfully or not. To verify it we curl the loadbalancer ipaddress from the NGINX loadbalancer and view the output that two webservers respond simultaneously .

How to configure NGINX load balancer on linux step by step guide

How to configure NGINX load balancer on linux step by step guide



[root@lb ~]# curl http://192.168.137.2
welcome to server1
[root@lb ~]# curl http://192.168.137.2
welcome to server2


If this article helpful to know about how to configure NGINX load balancer please share this.


SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment