Configure Nginx as HTTP Load Balancer in CentOS 7

Configure Nginx as HTTP Load Balancer in CentOS 7

Nginx is a free and open-source web server. Nginx can also be used as reverse proxy, load balancer, mail proxy and HTTP cache. Currently, it is the second most widely used web server over the Internet. Also, there are many web servers that are using Nginx as the Reverse Proxy and Load Balancer.

In this article, we will configure Nginx as a HTTP Load Balancer in CentOS 7.

 

This Article Provides:

     

    System Specification:

    In this article, we are using three virtual machines. Two VMs to deploy and run two websites and One VM to configure as the reverse proxy and HTTP load balancer.

    Hostname web-01.example.com web-02.example.com proxy-02.example.com
    IP Address 192.168.116.51/24 192.168.116.52/24 192.168.116.54/24
    Operating System CentOS 7.6 CentOS 7.6 CentOS 7.6
    Web Server Apache Apache Nginx

    We have already configured web-01.example.com and web-02.example.com as the web servers and hosted a simple and distinct webpage on both servers.

    Web-01-Home-PageWeb-02-Home-Page

     

    Installing Nginx Web Server on CentOS 7:

    Connect to proxy-02.example.com using ssh.

    Nginx can be installed from EPEL (Extra Packages for Enterprise Linux) yum Repository. Therefore, we have to install EPEL yum repository.

    [root@proxy-02 ~]# yum install -y epel-release
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    * base: mirrors.ges.net.pk
    * extras: mirrors.ges.net.pk
    * updates: mirrors.ges.net.pk
    Resolving Dependencies
    --> Running transaction check
    ---> Package epel-release.noarch 0:7-11 will be installed
    --> Finished Dependency Resolution

    Dependencies Resolved

    ================================================================================
    Package Arch Version Repository Size
    ================================================================================
    Installing:
    epel-release noarch 7-11 extras 15 k

    Transaction Summary
    ================================================================================
    Install 1 Package

    Total download size: 15 k
    Installed size: 24 k
    Downloading packages:
    epel-release-7-11.noarch.rpm | 15 kB 00:03
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
    Installing : epel-release-7-11.noarch 1/1
    Verifying : epel-release-7-11.noarch 1/1

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

    Complete!

    Let the yum create cache of repositories using following command.

    [root@proxy-02 ~]# yum makecache
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    epel/x86_64/metalink | 7.8 kB 00:00
    * base: mirrors.ges.net.pk
    * epel: kartolo.sby.datautama.net.id
    * extras: mirrors.ges.net.pk
    * updates: mirrors.ges.net.pk
    base | 3.6 kB 00:00
    epel | 4.7 kB 00:00
    extras | 3.4 kB 00:00
    updates | 3.4 kB 00:00
    (1/6): epel/x86_64/prestodelta | 1.5 kB 00:00
    (2/6): epel/x86_64/group_gz | 88 kB 00:02
    (3/6): epel/x86_64/updateinfo | 955 kB 00:05
    (4/6): epel/x86_64/other_db | 3.2 MB 00:36
    (5/6): epel/x86_64/primary_db | 6.6 MB 01:43
    (6/6): epel/x86_64/filelists_db | 11 MB 04:28
    Metadata Cache Created

    Install Nginx web server from EPEL yum repository.

    [root@proxy-02 ~]# yum install -y nginx
    ...
    Installed:
    nginx.x86_64 1:1.12.2-2.el7

    Dependency Installed:
    dejavu-fonts-common.noarch 0:2.33-6.el7
    dejavu-sans-fonts.noarch 0:2.33-6.el7
    fontconfig.x86_64 0:2.13.0-4.3.el7
    fontpackages-filesystem.noarch 0:1.44-8.el7
    gd.x86_64 0:2.0.35-26.el7
    gperftools-libs.x86_64 0:2.6.1-1.el7
    libX11.x86_64 0:1.6.5-2.el7
    libX11-common.noarch 0:1.6.5-2.el7
    libXau.x86_64 0:1.0.8-2.1.el7
    libXpm.x86_64 0:3.5.12-1.el7
    libjpeg-turbo.x86_64 0:1.2.90-6.el7
    libxcb.x86_64 0:1.13-1.el7
    nginx-all-modules.noarch 1:1.12.2-2.el7
    nginx-filesystem.noarch 1:1.12.2-2.el7
    nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7
    nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7
    nginx-mod-http-perl.x86_64 1:1.12.2-2.el7
    nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7
    nginx-mod-mail.x86_64 1:1.12.2-2.el7
    nginx-mod-stream.x86_64 1:1.12.2-2.el7
    perl.x86_64 4:5.16.3-293.el7
    perl-Carp.noarch 0:1.26-244.el7
    perl-Encode.x86_64 0:2.51-7.el7
    perl-Exporter.noarch 0:5.68-3.el7
    perl-File-Path.noarch 0:2.09-2.el7
    perl-File-Temp.noarch 0:0.23.01-3.el7
    perl-Filter.x86_64 0:1.49-3.el7
    perl-Getopt-Long.noarch 0:2.40-3.el7
    perl-HTTP-Tiny.noarch 0:0.033-3.el7
    perl-PathTools.x86_64 0:3.40-5.el7
    perl-Pod-Escapes.noarch 1:1.04-293.el7
    perl-Pod-Perldoc.noarch 0:3.20-4.el7
    perl-Pod-Simple.noarch 1:3.28-4.el7
    perl-Pod-Usage.noarch 0:1.63-3.el7
    perl-Scalar-List-Utils.x86_64 0:1.27-248.el7
    perl-Socket.x86_64 0:2.010-4.el7
    perl-Storable.x86_64 0:2.45-3.el7
    perl-Text-ParseWords.noarch 0:3.29-4.el7
    perl-Time-HiRes.x86_64 4:1.9725-3.el7
    perl-Time-Local.noarch 0:1.2300-2.el7
    perl-constant.noarch 0:1.27-2.el7
    perl-libs.x86_64 4:5.16.3-293.el7
    perl-macros.x86_64 4:5.16.3-293.el7
    perl-parent.noarch 1:0.225-244.el7
    perl-podlators.noarch 0:2.5.1-3.el7
    perl-threads.x86_64 0:1.87-4.el7
    perl-threads-shared.x86_64 0:1.43-6.el7

    Complete!

    Start and enable nginx.service.

    [root@proxy-02 ~]# systemctl start nginx.service
    [root@proxy-02 ~]# systemctl enable nginx.service
    Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

    Allow http service in Linux Firewall.

    [root@proxy-02 ~]# firewall-cmd --permanent --add-service=http
    success
    [root@proxy-02 ~]# firewall-cmd --reload
    success

    Browse URL http://proxy-02.example.com in a client browser.

    nginx-default-test-page-01.png

    Nginx Web Server has been installed.

     

    Configure Nginx as HTTP Load Balancer:

    Our Nginx web server is already configured and running at default HTTP port 80.Although, we can configure the same HTTP port as reverse proxy load balancer, but we will keep it clean and add new configurations for the port 8888.

    Create a new Nginx configuration file.

    [root@proxy-02 ~]# vi /etc/nginx/conf.d/app.conf

    Add following directives therein.

    upstream appset {
    server web-01.example.com;
    server web-02.example.com;
    }

    server {
    listen 8888;

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

    Adjust SELinux policy to allow Nginx to run HTTP service on port 8888.

    [root@proxy-02 ~]# semanage port -a -t http_port_t -p tcp 8888

    Allow service port 8888/tcp in Linux Firewall.

    [root@proxy-02 ~]# firewall-cmd --permanent --add-port=8888/tcp
    success
    [root@proxy-02 ~]# firewall-cmd --reload
    success

    Restart nginx.service.

    [root@proxy-02 ~]# systemctl restart nginx.service

    Browse URL http://proxy-02.example.com:8888/ in a client browser.

    Web-02-Home-Page

    Our request has been served by web-02.example.com.

    Refresh webpage again.

    Web-01-Home-Page

    Now it forwards, our request to web-01.example.com.

    We have configured a Reverse Proxy and Load Balancer using Nginx Web Server. Here, the configurations are basic and are solely for the demonstration purpose. However, you can ammend the same configurations according to your environment to create a relatively advanced Load Balancer.

    0 Comments