Skip to content

Latest commit

 

History

History

06-Implementing-Load-Balancing-with-Nginx

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Implementing Load Balancers with Nginx

Introduction to Load Balancing and Nginx

Load balancing refers to efficiently distributing incoming network traffic across a group of backend servers, also known as a server farm or server pool.

Modern high‑traffic websites must serve hundreds of thousands, if not millions, of concurrent requests from users or clients and return the correct text, images, video, or application data, all in a fast and reliable manner. To cost‑effectively scale to meet these high volumes, modern computing best practice generally requires adding more servers.

A load balancer acts as the “traffic cop” sitting in front of your servers and routing client requests across all servers capable of fulfilling those requests in a manner that maximizes speed and capacity utilization and ensures that no one server is overworked, which could degrade performance. If a single server goes down, the load balancer redirects traffic to the remaining online servers. When a new server is added to the server group, the load balancer automatically starts to send requests to it.

load balancer

In this manner, a load balancer performs the following functions:

  • Distributes client requests or network load efficiently across multiple servers.
  • Ensures high availability and reliability by sending requests only to servers that are online.
  • Provides the flexibility to add or subtract servers as demand dictates.

Nginx is a versatile software, it can act like a web server, reverse proxy and a load balancer depending on configuration.

Implementing Nginx as a Basic Load Balancer between Two Web Servers

Prerequisite

  1. Ensure you have an AWS account. If you don't have an account, sign up for AWS here.

The following steps are taken to implement Nginx as a basic load balancer between two web servers:

Step 1: Provisioning the 1st Apache Web Server

  • Open your AWS Management Console, click on EC2.

aws console

  • Click on the Launch Instance button.

lauch instance

  • On the Name Box and Amazon Machine Image, type Apache_Web_Server_01 and ubuntu respectively.

name box ami

  • Select Ubuntu Server 22.04 LTS (HVM), SSD Volume Type as the Amazon Machine Image.

ubuntu server

  • Click on create new key pair.

key pair

  • Give the key pair name a name of your choice (i.e web11), select RSA as the key pair type and .pem as the key file format then click on Create key pair.

rsa pem

Note that the .pem key will be downloaded into your Downloads directory on your computer.

  • On the Network Settings tab, click on the Edit button to configure the Security Group Inbound Rules.

edit security group

  • Click on Add Security Group Rule on the bottom and the Security Group Name, Source Type and Port Range type the following: Apache_Server_Security_Group, Anywhere and 8000 respectively.

security group parameters

  • Scroll down to the bottom and click on the Launch Instance Button.

lauch instance button

  • You will see a prompt shown below, click on the Instance ID highlighted.

instance prompt

  • Click on the Instance ID of the Apache_Web_Server_01 Instance you just created.

instance id

  • Click on the Connect button.

connect button

  • Copy the highlighted commands shown below to connect to the Instance:

highlighted commands

  • Open your terminal.

  • Go to the Downloads directory (i.e .pem key pair is stored here) using the command shown below:

cd Downloads

cd downloads

  • Run the following command to give read permissions to the .pem key pair file:
chmod 400 <private-key-pair-name>.pem

chmod keypair

  • SSH into the Apache_Web_Server_01 Instance using the command shown below:
ssh -i <private-key-name>.pem ubuntu@<Public-IP-address>

ssh apache instance

  • Update the list of packages in the package manager and install the apache server package installation using the following command:
sudo apt update && sudo apt install apache2 -y

update install apache

  • Verify that Apache is running using the command shown below:
sudo systemctl status apache2

systemctl status apache

Step 2: Configuring the 1st Apache Server to Serve Content on Port 8000

  • Run the following command to open the Apache listening ports configuration file:
sudo vi /etc/apache2/ports.conf
  • Add a new Listen directive for port 8000 as shown below and save the file:

vi ports.conf

  • Run the following command to open the default configuration file of Apache.
sudo vi /etc/apache2/sites-available/000-default.conf
  • Change the listening port of the Virtualhost from 80 to 8000 as shown below and save the file:

vi default.conf

  • Reload the Apache Web Server to load the new configuration changes using the command shown below:
sudo systemctl reload apache2

systemctl reload apache

Step 3: Creating a Webpage for the 1st Apache Web Server

  • Create and open a new index.html file with the command shown below:
sudo vi index.html
  • Before pasting the html code block shown below, get the Public IPv4 address of the Apache_Web_Server_01 by clicking the Instance ID of the Instance and copying the address.
        <!DOCTYPE html>
        <html>
        <head>
            <title>My EC2 Instance</title>
        </head>
        <body>
            <h1>Welcome to my Apache Web Server 1 EC2 instance</h1>
            <p>Public IP: YOUR_PUBLIC_IP</p>
        </body>
        </html>

index html

  • Change the file ownership of the index.html file so that the Nginx Load Balancer Server can have access to the file using the command shown below:
sudo chown www-data:www-data ./index.html

chown www-data index.html

  • Overwrite the default html file of the Apache Web Server by using the command shown below:
sudo cp -f ./index.html /var/www/html/index.html

cp index.html

  • Reload the Apache Web Server to load the new configuration changes using the command shown below:
sudo systemctl reload apache2

systemctl reload apache

  • Go to your your browser and paste the following URL:
http://<public_ip_address_of_apache_web_server_1>:8000

http ip url1

The Web Page Should Look Like This

Step 4: Provisioning and Configuring the 2nd Apache Web Server

Repeat steps 1 - 3 but ensure the following parameters are changed to these when implementing the steps:

  1. Name of the Instance: Apache_Web_Server_02

  2. Key pair: web11

  3. Security Group: Apache_Server_Security_Group

  4. When serving content for the 2nd Apache Web Server's webpage, use this as your html code block:

        <!DOCTYPE html>
        <html>
        <head>
            <title>My EC2 Instance</title>
        </head>
        <body>
            <h1>Welcome to my Apache Web Server 2 EC2 instance</h1>
            <p>Public IP: YOUR_PUBLIC_IP</p>
        </body>
        </html>

index html2

After completing the steps, go to your browser and paste the following URL:

http://<public_ip_address_of_apache_web_server_2>:8000

http ip url2

The Web Page Should Look Like This

Step 5: Provisioning the Nginx Load Balancer Server

  • On the Instances tab, click on the Launch Instance button.

  • On the Name Box and Amazon Machine Image, type Nginx Load Balancer Server and ubuntu respectively.

name box ami

  • Select Ubuntu Server 22.04 LTS (HVM), SSD Volume Type as the Amazon Machine Image.

ubuntu server 22

  • Click on the key pair drop-down button and select web11 as the key pair.

keypair drop down

  • Create a new security group and select allow HTTP traffic from the internet.

create security group

  • Click on the Launch Instance button.

launch instance button

  • You will see a prompt shown below, click on the Instance ID highlighted.

prompt instance

  • Click on the Instance ID of the Nginx Load Balancer Server Instance you just created.

instance id

  • Click on the Connect button.

connnect button

  • Copy the highlighted command shown below to connect to the Instance:

highlighted command

  • Open your terminal.

  • Go to the Downloads directory (i.e .pem key pair is stored here) using the command shown below:

cd Downloads

cd downloads

  • SSH into the Nginx Load Balancer Server Instance using the command shown below:
ssh -i <private-key-name>.pem ubuntu@<Public-IP-address>

ssh pem key

  • Update the list of packages in the package manager and install the apache server package installation using the following command:
sudo apt update && sudo apt install nginx -y

update and install nginx

  • Verify that Nginx is running using the command shown below:
sudo systemctl status nginx

systemctl status nginx

Step 6: Configuring Nginx as the Load Balancer between the Two Apache Web Servers

  • Run the following command to create and open a load balancer configuration file:
sudo vi /etc/nginx/conf.d/loadbalancer.conf
  • Paste the code block shown below and save the file:
        upstream backend_servers {

            # your are to replace the public IP and Port to that of your web servers
            server 127.0.0.1:8000; # public IP and port for web server 1
            server 127.0.0.1:8000; # public IP and port for web server 2

        }

        server {
            listen 80;
            server_name <your_load_balancers_public_ip_addres>; # provide your load balancers public IP address

            location / {
                proxy_pass http://backend_servers;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
        }

loadbalance conf

The upstream backend_servers block defines a group of backend servers. The server directive lists the addresses and ports of your backend server. The proxy_pass directive ins the location block sets up load balancing and passes trequests to the backend servers. The proxy_set_header directives pass necessary headers to the backend servers to correctly handle requests.

  • Run the following command to test if the Nginx Load Balancer Server's configuration was successful:
sudo nginx -t

nginx -t

  • Reload the Nginx Load Balancer Server to load the new configuration changes using the command shown below:
sudo nginx -s reload

nginx -s reload

  • Go to your browser and paste the URL shown below:
http://<public_ip_address_of_nginx_load_balancer_server>:80

http ip1

http ip2

You will notice that each time you refresh the page, it displays the content of one of the two Apache Web Servers. Note that the Load Balancing Algorithm used in configuring the Nginx Load Balancer Server is the Round Robin Algorithm.