Nginx is a popular open-source web server software that can also be used as a load balancer. Load balancing is a technique that distributes incoming traffic across multiple servers to improve performance, increase availability, and avoid overloading any single server. In this article, we’ll discuss how to configure Nginx as a load balancer with an example.
To configure Nginx as a load balancer required the following steps:
- Install Nginx web server
- Configure the upstream and add all nodes of backend servers
- Setup proxy_pass location with the upstream
- Restart the Nginx server to apply changes
- Test the Nginx load balancer setup
Let’s follow the steps one by one to complete Nginx configuration to act as an load balancer.
Step 1: Install Nginx
The first step is to install Nginx on the server that will act as the load balancer. Nginx can be installed on most Linux distributions using the package manager. For example, on Ubuntu, you can run the following command:
sudo apt-get update
sudo apt-get install nginx
On RHEL-based systems you can use the following command to install Nginx:
sudo yum install nginx
The modern RHEL-based system uses DNF package manager.
Step 2: Configure Upstream Servers
The next step is to define the servers that will receive traffic from the load balancer. These servers are known as upstream servers. You can define them in the Nginx configuration file /etc/nginx/nginx.conf. Open the file in a text editor and add the following code:
1 2 3 4 5 6 | http { upstream myapp { server 192.168.10.201; server 192.168.10.202; } } |
In this example, we’ve defined an upstream group named “myapp” that includes two servers: 192.168.10.201 and 192.168.10.202. These are the servers that will receive traffic from the load balancer.
Step 3: Configure Load Balancer
Now that we’ve defined our upstream servers, we can configure the load balancer. Add the following code to the http block in nginx.conf:
1 2 3 4 5 6 7 8 9 10 11 12 13 | http { upstream myapp { server 192.168.10.201; server 192.168.10.202; } server { listen 80; location / { proxy_pass http://myapp; } } } |
In this code, we’ve defined a server block that listens on port 80 (the default HTTP port). The location / block specifies the URL path that should be routed to the upstream servers. The proxy_pass directive tells Nginx to forward incoming requests to the “myapp” upstream group we defined earlier.
Step 4: Restart Nginx
Finally, restart Nginx to apply the changes to the configuration file. Run the following command:
sudo service nginx restart
Step 5: Testing the Load Balancer
To test the load balancer, we can use a web browser or a tool like cURL to send HTTP requests to the server. When we access the URL configured in the location / block, Nginx will forward the request to one of the upstream servers in the “myapp” group.
If everything is working correctly, you should see the response from one of the upstream servers. You can confirm that the load is being balanced across both servers by refreshing the page multiple times and checking the IP address of the server that responds to each request.
Conclusion
Configuring Nginx as a load balancer is a straightforward process that can help improve the performance and availability of web applications. By defining upstream servers and configuring the load balancer, you can distribute incoming traffic across multiple servers and avoid overloading any single server.
1 Comment
Hi RAHUL,
I have to create the load balancing in the /etc/nginx/conf.d/ or /etc/nginx/sites-available
when i configure the load balancer from i getting 502 Bad Gateway can you help me into this.