Dump the code

Load balancing configurations

Created 8 months ago
Posted By admin
4min read
Load balancing is a technique used to distribute incoming network traffic across multiple servers. In the context of web servers, it helps to ensure that no single server becomes overwhelmed with too much traffic, improving both performance and reliability. Nginx, a popular web server and reverse proxy server, provides robust support for load balancing.

Basic load balancing configuration:

Edit the Nginx configuration file. On Ubuntu, the main configuration file is typically located at /etc/nginx/nginx.conf or /etc/nginx/sites-available/default. Alternatively, you can create a new configuration file in the sites-available directory.

sudo nano /etc/nginx/sites-available/load-balancer

Here's a basic example of a load balancing configuration using the upstream directive:

     upstream backend {
         server  backend1.example.com;
         server  backend2.example.com;
         # Add more backend servers as needed
     }

     server {
         listen 80;
         server_name your_domain.com;

         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;
         }
     }
  • upstream: Defines a group of backend servers.
  • server: Specifies the backend server's address and port.
  • proxy_pass: Distributes requests among the backend servers.
  • proxy_set_header: Sets headers to pass additional information to the backend server.

The backend in the upstream backend block and the proxy_pass http://backend line must match. The upstream block defines a group of backend servers under the name backend, and the proxy_pass directive uses that name to refer to the backend servers.

So, in this case, proxy_pass http://backend; is telling Nginx to pass the requests to the backend servers defined in the upstream backend block.

If you change the name in the proxy_pass directive, it should match the name used in the upstream block. Consistency in naming helps maintain clarity in the configuration and ensures that Nginx correctly directs requests to the specified upstream group.

Final steps:

Create a symbolic link to enable the site configuration:
sudo ln -s /etc/nginx/sites-available/load-balancer /etc/nginx/sites-enabled/

Ensure there are no syntax errors in your Nginx configuration:
sudo nginx -t

Reload Nginx:
sudo systemctl reload nginx

Types of load balancing algorithms:

Nginx supports various load balancing algorithms to determine how it distributes requests among the backend servers. Some common algorithms include:

  • round-robin: Distributes requests sequentially to the servers.
  • least_conn: Sends requests to the server with the least number of active connections.
  • ip_hash: Ensures that requests from the same IP address always go to the same server.

To specify a load balancing algorithm, you can add it to the upstream block:

upstream backend {
    least_conn;
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com;
}


This basic example provides a starting point for load balancing with Nginx. Depending on your requirements and the scale of your application, you may need to explore additional configurations and optimizations.
Topics

Mastering Nginx

27 articles

Bash script

2 articles

Crontab

2 articles