Using Nginx As HTTP Load Balancer (original) (raw)

Last Updated : 5 Mar, 2024

Load balancing is a technique used in modern application servers for fault-tolerant systems. The load balancer is a networking device that splits network traffic across multiple backend servers so that the load on each server becomes constant making the overall system persistent and fault-tolerant. Nginx server is an enterprise-grade web server that is widely used across the industry. In this article, we will see how to configure nginx for HTTP load balancing.

Primary Terminologies

Using Nginx as HTTP load balancer:

**NOTE: For this article, we will create Virtual Machines in Azure for use as servers. You can also use local machines for configuration.

Step 1: Create and set up servers.

Virtual Machines

Creating Virtual Machines

Selecting Inbound Ports

Step 2: Install and Configure Nginx.

SSH To VM

sudo apt update

Update the Package manager

sudo apt install nginx

Install Plugin

systemctl status nginx

Nginx Status

Nginx

Step 3: Configure Nginx Web pages.

sudo su

nano index.debian.html

Hello From Server 1

Routing the Server

Accessing the Pages

Server 1

Screenshot-(351)

Step 4: Configure Load balancer.

nano nginx.conf

include /etc/nginx/conf.d/.conf;
include /etc/nginx/sites-enabled/
;

Edit the Config Files

upstream backend {
least_conn;
server <PUBLIC IP OF SERVER 1>;
server <PUBLIC IP OF SERVER 2>;
}

server {
listen 80;
server_name ;

            location / {  
                    proxy_pass http://backend;  
                    proxy_set_header Host $host;  
                    proxy_set_header X-Real-IP $remote_addr;  
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
                    proxy_set_header X-Forwarded-Proto $scheme;  

}

Configuration files

systemctl restart nginx

Restart the Nginx

Step 5 : Test the load balancer

Server 2

Conclusion

Thus, we have configured HTTP load balancer with the help of NGINX server. We have split the traffic across two backend server which can be edited by adding more backend server .More configuration can be added to configure load balancer for other purposes.

Are there alternatives to Nginx for HTTP load balancing?

Yes, there are alternative load balancing solutions, including HAProxy, Apache HTTP Server with mod_proxy, and cloud-specific load balancers provided by cloud service providers (e.g., AWS Elastic Load Balancing, Azure Load Balancer).

Can I use Nginx as a load balancer in a microservices architecture?

Yes, Nginx is well-suited for load balancing in a microservices environment. It can be used to distribute traffic among multiple microservices, providing flexibility and scalability in handling diverse workloads.

How do I monitor and troubleshoot Nginx load balancing?

Nginx provides various log files for monitoring and diagnostics. Additionally, external monitoring tools, Nginx status modules, and access to error logs can be valuable for identifying and resolving issues. Regularly reviewing logs and metrics is essential for effective troubleshooting.

Is Nginx suitable for large-scale deployments and high traffic websites?

Yes, Nginx is renowned for its ability to handle a large number of concurrent connections and high traffic volumes. It is widely used by websites and applications with high traffic demands due to its efficiency and low resource usage.

How does Nginx handle load balancing?

Nginx uses load balancing algorithms (e.g., round-robin, least_conn, ip_hash) to distribute incoming requests among a group of backend servers defined in the configuration. The proxy_pass directive is commonly used to forward requests to the specified upstream group.