Loadblance

How to Set Up Nginx Load Balancing on Ubuntu 24.04

As businesses increasingly move their applications to the cloud, the need for performant and highly available systems grows. One effective way to ensure your application can handle high traffic and maintain uptime is to implement load balancing. In this blog post, we will walk you through the process of setting up Nginx as a load balancer on Ubuntu 24.04. This solution will help you efficiently distribute network traffic across multiple servers, enhancing reliability and performance.

What is Load Balancing?

Load balancing is the process of distributing network or application traffic across multiple servers. This ensures that no single server becomes overwhelmed with requests, which can lead to slow response times or server failure. By distributing the workload, load balancers can increase reliability and efficiency while providing redundancy in case of server failure.

Prerequisites

Before we start, ensure you have the following:

  1. Ubuntu 24.04: An instance of Ubuntu Server 24.04.
  2. Nginx: Installed on your load balancer server.
  3. Backend Servers: At least two application servers that Nginx can distribute requests to. These could be additional instances of Ubuntu 24.04 running your web application.
  4. Root Access: Access to your server as a superuser or root.

Step 1: Installing Nginx

First, we need to install Nginx on our load balancer server. Open your terminal and run the following commands:

sudo apt update
sudo apt install nginx

Once installed, you can start and enable Nginx to run on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

You can verify that Nginx is running by visiting your server’s public IP address in a web browser. You should see the default Nginx welcome page.

Step 2: Configuring Nginx as a Load Balancer

Next, we’ll configure Nginx to act as a load balancer. We’ll define a server block that forwards requests to our backend application servers.

  1. Create a new configuration file for your load balancer settings. You can name this file load_balancer.conf:
sudo nano /etc/nginx/conf.d/load_balancer.conf
  1. Add the following configuration to the file, replacing the backend_server1_ip and backend_server2_ip with the IP addresses of your application servers:
http {
    upstream backend {
        server backend_server1_ip;
        server backend_server2_ip;
    }

    server {
        listen 80;

        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;
        }
    }
}

This configuration defines an upstream block called backend that includes two server instances. The load balancer listens on port 80, and all incoming requests are forwarded to one of the backend servers.

Step 3: Testing Your Configuration

Now that you have configured Nginx as a load balancer, it’s time to test your setup.

  1. Test the Nginx configuration to ensure there are no syntax errors:
sudo nginx -t

If the output indicates that the configuration is okay, proceed to the next step.

  1. Reload Nginx to apply the changes:
sudo systemctl reload nginx
  1. Access your load balancer by navigating to your load balancer server’s public IP address in a web browser. You should see the application running on one of your backend servers.
  2. To test load balancing, refresh the page multiple times. You should see traffic being distributed to the different backend servers based on the configuration settings you used.

Step 4: Fine-tuning the Load Balancer

Nginx provides several methods to control how the load is balanced among your backend servers. Here are a few alternatives you can consider:

  1. Round Robin (default): Distributes requests evenly to each server.
  2. Least Connections: New connections are sent to the server with the least active connections.
  3. IP Hash: Ensures that requests from a specific IP address are always sent to the same server.

To modify the load balancing method, you can adjust the upstream configuration. For example, to use least connections, update the upstream block as follows:

upstream backend {
    least_conn;
    server backend_server1_ip;
    server backend_server2_ip;
}

Don’t forget to reload Nginx after making any changes.

Conclusion

Setting up Nginx as a load balancer on Ubuntu 24.04 is a straightforward process that enhances the availability and scalability of your applications. By distributing incoming traffic evenly across multiple application servers, you can improve performance and reduce the risk of outages.

As your application grows, consider adding more backend servers or implementing SSL termination, session persistence, or health checks for further optimization. For more advanced configurations, Nginx offers extensive documentation that can help you customize your load balancing further.

Happy Load Balancing!