Latest

Monday, 21 January 2019

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.

 

Table of Contents:

Configure Nginx as HTTP Load Balancer in CentOS 7

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.

# yum install -y epel-release

Let the yum create cache of repositories using following command.

# yum makecache

Install Nginx web server from EPEL yum repository.

# yum install -y nginx

Start and enable nginx.service.

# systemctl start nginx.service
# 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.

# firewall-cmd --permanent --add-service=http
success
# 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.

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

# semanage port -a -t http_port_t -p tcp 8888

Allow service port 8888/tcp in Linux Firewall.

# firewall-cmd --permanent --add-port=8888/tcp
success
# firewall-cmd --reload
success

Restart nginx.service.

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

If you find this article useful? Don't you mind support us by Buy Me A Coffee


3 comments:

  1. after installation when I try to access it through web it is not accessible .. after this when i configure upstraem servers ip/names its status is failed..then i remove all of its configuration then its status is running i dont why ?

    ReplyDelete
    Replies
    1. Have a look into log files for any errors.

      Delete

© 2022 CentLinux. All Rights Reserved.