
Nginx is a free, open-source, high-performance server for web serving, reverse proxying, caching, load balancing, media streaming, and more. Its event-based, asynchronous architecture has made it one of the most popular and best-performing web servers available.
Nginx can be configured as a load balancer to distribute incoming traffic and requests among a group of application instances. Load balancing across multiple application instances is a commonly used technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault-tolerant configurations. It is an excellent way to scale a web application and increase its performance and redundancy.
A load-balancing algorithm is a logic that a load balancer uses to distribute network traffic between servers. Nginx provides 3 algorithms for balancing the load between a group of servers.
Round Robin (Default) - Round Robin is the default load‑balancing technique for Nginx. The load balancer runs through the list of upstream servers in sequence, assigning the next connection request to each one in turn.
Least Connections - With the Least Connections method, the load balancer compares the current number of active connections it has to each server and sends the request to the server with the fewest connections. You configure it with the least_conn directive.
IP Hash - IP Hash is a predefined variant of the Hash method, in which the hash is based on the client's IP address. You set it with the ip_hash directive.
Upstream block in Nginx is used to define a group of servers running our application. You can also mention the traffic distribution method inside it.
Servers are listed inside the upstream block using the server directive and can be identified using IP address, hostname, or UNIX socket path.
Servers identified by Hostname.
Servers identified by UNIX Socket Path.
weight parameter in upstream server directive can be defined to configure weighted load balancing.
Following directives can be used to define traffic distribution method in an upstream block:
Example:
If you don't define a method it will default to round-robin algorithm, which is used to distribute traffic among servers sequentially.
Server block in Nginx is used to define a virtual server that listens for traffic with the characteristics that are defined in it.
In our example, the given server block listens for incoming traffic on domain example.com and proxies it to an upstream named backend.
location directive is used to define settings/rules for a folder/directory on your server. Directives that can be used inside a location directive to route traffic to an upstream are as follows:
Install Nginx on Ubuntu or Debian
Install Nginx on CentOS
Assuming your web application is running on 3 servers with IP addresses in the range 10.0.0.1 to 10.0.0.3 and the method desired is round-robin, then our upstream block should look like the following.
Here we have used the name backend to define the upstream, which we can use to pass the requests to our cluster. To pass the requests to our server block should look like the following.
Create a new vhost.
Paste the following content and save the file using Ctrl + X then Enter
Add a soft link of the vhost file in sites-enabled directory.
Test the configuration.
Reload Nginx service.
By following these steps successfully, your cluster now should be running and accessible on the following link.
Make sure
your_domainpoints to the server in which we configured our load balancer using an A record.
For demonstration purposes, we configured 3 servers with an index.html containing H1 tag with the name of the machine to identify the selected server. As mentioned in the steps given above, we used the round-robin method, so it used servers listed in our upstream sequentially.
We will use Let's Encrypt to obtain an SSL Certificate for free. Please make sure you have pointed your domain to the server's IP address.
Install Certbot package.
Install SSL on your domain.
You can verify if the SSL Certificate is configured properly or not by opening the following link in your web browser.
Let's Encrypt certificates are only valid for 90 days, but since we are using certbot, it will handle auto-renewals for us.
Verify if the auto-renewal works.
If the above command doesn't throw an error, it means your SSL certificate will be renewed automatically without any issues.
In this article, you understood the basics of load balancing and learned how to configure a load balancer using Nginx and secure it using an SSL Certificate.
0 Comments
Be the first to comment and share your perspective with the community.