How to configure Nginx Load Balancer in CentOS

To configure Nginx Load Balancer in CentOS

Nginx is an open source high performance web server that works in the HTTP protocol. 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 CentOS.

Prerequisites

For hostname resolution you need to have DNS server or make entry in /etc/hosts file.

Host file Entry:

192.168.5.157 ha.example.com ha
192.168.5.158 ws1.example.com ws1
192.168.5.159 ws2.example.com ws2

Testing Environment

Load Balancer:

IP Address &ndash 192.168.5.157
Hostname &ndash ha.example.com

WebServer 1:

IP Address &ndash 192.168.5.158
Hostname &ndash ws1.example.com

WebServer 2:

IP Address &ndash 192.168.5.159
Hostname &ndash ws2.example.com


Installation of Nginx

Run the below command to install epel repository.

[root@ha ~]# yum install epel-release -y
Loaded plugins: fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base                                                                                                                                                                  | 3.6 kB  00:00:00     
extras                                                                                                                                                                | 3.4 kB  00:00:00     
updates                                                                                                                                                               | 3.4 kB  00:00:00     
(1/2): extras/7/x86_64/primary_db                                                                                                                                     | 166 kB  00:00:06     
(2/2): updates/7/x86_64/primary_db                                                                                                                                    | 9.1 MB  00:00:54     
Determining fastest mirrors
 * base: centos.excellmedia.net
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
Resolving Dependencies
-->  Running transaction check
--->  Package epel-release.noarch 0:7-6 will be installed
-->  Finished Dependency Resolution
.
.
.
Transaction test succeeded
Running transaction
  Installing : epel-release-7-6.noarch                                                                                                                                                   1/1
  Verifying  : epel-release-7-6.noarch                                                                                                                                                   1/1

Installed:
  epel-release.noarch 0:7-6                                                                                                                                                                  

Complete!

Now its time to install the Nginx as shown below,

[root@ha ~]# yum install nginx -y
Loaded plugins: fastestmirror, langpacks
epel/x86_64/metalink                                                                                                                                                  | 5.2 kB  00:00:00     
http://epel.mirror.net.in/epel/7/x86_64/repodata/repomd.xml: [Errno 14] curl#7 - " Failed connect to epel.mirror.net.in:80  Connection refused" 
Trying other mirror.
epel                                                                                                                                                                  | 4.3 kB  00:00:00     
(1/3): epel/x86_64/group_gz                                                                                                                                           | 170 kB  00:00:02     
(2/3): epel/x86_64/updateinfo                                                                                                                                         | 673 kB  00:00:13     
(3/3): epel/x86_64/primary_db                                                                                                                                         | 4.3 MB  00:00:47     
Loading mirror speeds from cached hostfile
 * base: centos.excellmedia.net
 * epel: kartolo.sby.datautama.net.id
 * extras: centos.excellmedia.net
 * updates: centos.excellmedia.net
Resolving Dependencies
-->  Running transaction check
--->  Package nginx.x86_64 1:1.10.1-1.el7 will be installed
.
.
.
Installed:
  nginx.x86_64 1:1.10.1-1.el7                                                                                                                                                                

Dependency Installed:
  GeoIP.x86_64 0:1.5.0-9.el7                         nginx-all-modules.noarch 1:1.10.1-1.el7    nginx-filesystem.noarch 1:1.10.1-1.el7            nginx-mod-http-geoip.x86_64 1:1.10.1-1.el7
  nginx-mod-http-image-filter.x86_64 1:1.10.1-1.el7  nginx-mod-http-perl.x86_64 1:1.10.1-1.el7  nginx-mod-http-xslt-filter.x86_64 1:1.10.1-1.el7  nginx-mod-mail.x86_64 1:1.10.1-1.el7       
  nginx-mod-stream.x86_64 1:1.10.1-1.el7            

Complete!


To configure Load Balancer in Nginx

Configure the load balancer, once the Nginx is installed successfully.

[root@ha ~]# vim /etc/nginx/nginx.conf

Comment some lines in the Nginx default configuration file to configure Nginx as a Load Balancer. .

#    server {
#        listen       80 default_server 
#        listen       [::]:80 default_server 
#        server_name  _ 
#        root         /usr/share/nginx/html 

        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf 

#        location / {
#        }

#        error_page 404 /404.html 
#            location = /40x.html {
#        }

#        error_page 500 502 503 504 /50x.html 
#            location = /50x.html {
#        }
#    }

Then add the required lines in the below entry and mention the web server' s IP address in it.

Entry:

# Define which servers to include in the load balancing scheme.
# It' s best to use the servers'  private IPs for better performance and security.
# You can find the private IPs at your UpCloud Control Panel Network section.

upstream backend {
   server 192.168.5.158 
   server 192.168.5.159 
}

# This server accepts all traffic to port 80 and passes it to the upstream.
# Notice that the upstream name and the proxy_pass need to match.

server {
   listen 80 

   location / {
      proxy_pass http://backend 
   }
}

Utilize the following command to start the Nginx service. Also add the firewall rule for outside connections.

[root@ha ~]# systemctl start nginx
[root@ha ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@ha ~]# firewall-cmd --permanent --add-service=http
success
[root@ha ~]# firewall-cmd --reload  
Success

For each and every session it will connect to different web servers that are added in load balancer.

Once the load balancer is configured, shoot-out the web browser with respective IP address.

At this point, when you hit the reload button to display the content from another server.

Save

Comment
amithalder
Dec 09 2019
Update the lines as following in config file.

upstream backend {
server 192.168.5.158;
server 192.168.5.159;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
Add a comment
FAQ
Q
Does NGINX Plus support load balancing for mail protocols?
A
Nginx support load balancing for HTTP/HTTPS/FastCGI/uwsgi/SCGI/memcache only. Nginx proxy and route SMTP/POP3/IMAP traffic to a single upstream rather than load-balancing it.
Q
Is it safe to use the development branch in production in NGiNX?
A
In general, all releases (development or otherwise) are quite stable. This site runs the latest development version at all times. Many NGINX users tend to represent an “early adopter” crowd,
Q
How can I deploy NGINX Plus for HTTP video?
A
Yes, NGINX Plus is a very popular solution for HTTP video distribution and a great, high-performance, highly scalable, extremely cost-efficient alternative to Wowza or Adobe products.
Q
is possible to configure HAProxy Load Balancer in Nginx?
A
Yes, It is possible to load the HAProxy load balancer in the NGiNX.
Q
What is thePurpose of using the Nginx?
A
Nginx is an open source high performance Load balancer and web server software. It supports HTTP, HTTPS, SMTP, POP3, and IMAP protocols as a reverse proxy server.