Load Balancing for Static Website Using Nignx
Mastering Load Balancing Algorithms, Exploring Nginx Load Balancing, and Implementing Various Load Balancing Methods
Load Balancing : Load balancing is the process of distributing network traffic across multiple servers. This ensures no single server bears too much demand. By spreading the work evenly, load balancing improves application responsiveness. It also increases availability of applications and websites for users.
Essentially, it balances incoming requests and then, depending on the load balancer's algorithm, directs them to the appropriate backend.
Methods of Load Balancing
Load balancing methods use different algorithms to handle various situations efficiently.
Least Connection Method: This method sends traffic to the server with the fewest active connections. It's great for scenarios with many persistent connections unevenly spread across servers.
Least Response Time Method: Traffic is directed to the server with both the fewest active connections and the lowest average response time.
Round Robin Method: Servers are rotated, with traffic sent to the first available server and then moved to the end of the queue. It works well when servers have similar specifications and there aren't many persistent connections.
IP Hash: The client's IP address determines which server receives the request.
Practical Demonstration: Implementing Nginx Load Balancing
Let's talk about the implementation of Nginx load balancing. I have set up cloud compute machines, with two servers designated for the Nginx backend and one server for handling load balancing.
After spinning up all three servers, I installed Nginx on each of them. We are using Nginx for load balancing and also as a content delivery network for our static site.
# Update and upgrade package repositories and packages
apt update -y && apt upgrade -y
# Install Nginx on the server
apt install nginx -y
After following the previous steps, clone a static site to your backend server. Alternatively, you can use the provided simple HTML content to ensure you can identify which backend server is delivering the content. Use the following command to set this content.
sudo bash -c 'cat > /var/www/html/index.nginx-debian.html <<EOF
Hello World (*-*)! From Backend Server
EOF'
# At the end of the server name, add 1 or 2 to ensure they can be differentiated.
Here are the expected outputs from each backend server if you follow the steps similar to me.
After cloning the static site to both backend servers and ensuring that Nginx is delivering the content correctly, verify that the Nginx configuration file is correctly set up. You can refer to my blog "Setup a Static Website Using Nginx" for guidance.
Next, configure the load balancer instance to balance the load between both servers.
# Define upstream block for backend servers
upstream backend {
server xx.xx.xx.xx;
server xx.xx.xx.xx;
}
# Configure the server block to listen on port 80
server {
listen 80;
# Define location block to proxy requests to the backend servers
location / {
proxy_pass http://backend;
}
}
Create a new configuration file in /etc/nginx/sites-available
and then enable it. You can refer to "Setup a Static Website Using Nginx" for guidance. After making these changes, access the IP address in a browser to verify the setup.
You can observe that both servers deliver content on the same IP address using the Nginx load balancer. By default, Nginx utilizes the round-robin method for load balancing unless explicitly specified.
To implement a different method, include the desired method type in the upstream block:
upstream backend {
least_conn;
server backend1.example.com;
server backend2.example.com;
}
For the IP hash method, replace least_conn
with ip_hash
:
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
}
There are other methods available, for more detailed information, you can refer to the Nginx documentation. This will provide a more comprehensive understanding of using reverse proxy.
That wraps up today's article.
Connect with me
Let's connect with me on Social Media