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.




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

Host file Entry: ha ws1 ws2


Testing Environment

Load Balancer:

IP Address –
Hostname –

WebServer 1:

IP Address –
Hostname –

WebServer 2:

IP Address –
Hostname –

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:
 * extras:
 * updates:
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

  epel-release.noarch 0:7-6                                                                                                                                                                  


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 [Errno 14] curl#7 - "Failed connect to; 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:
 * epel:
 * extras:
 * updates:
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.10.1-1.el7 will be 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            



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.


# 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 {

# 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/ to /usr/lib/systemd/system/nginx.service.
[root@ha ~]# firewall-cmd --permanent --add-service=http
[root@ha ~]# firewall-cmd --reload  

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.



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
Be the first to comment, we value your suggestions. For further queries please comment below.

Tags: Nginx CentOS Load balancer
user image Author :  Gabriel