nginx Multiple Load Balancing

Time:2024-5-28

I. Concepts

Nginx load balancing is a widely used technique in distributed applications to improve application availability and performance, and to guarantee system stability and reliability by achieving balanced distribution of requests.

Nginx load balancing is a technique for centralizing requests to multiple servers. Load balancing enables requests to be distributed across different servers, thus avoiding overloading a single server and improving application availability and performance.

Nginx load balancing achieves load balancing by using one server as a proxy server and forwarding requests to multiple back-end servers. In load balancing, the proxy server forwards client requests to different back-end servers according to a certain load balancing algorithm, and then returns the response to the client. This avoids overloading a particular server and makes the load of the entire application more balanced, improving application availability and performance.

Nginx supports a variety of load balancing algorithms, such as polling, IP hashing, least-connected, weighted polling, and so on. Each algorithm has its own specific application scenarios, advantages and disadvantages, and system administrators can choose the appropriate algorithm according to different needs.

II. Multiple strategies

There are several types of Nginx load balancing:

  1. Round Robin: This is the simplest form of load balancing, where requests are distributed to each proxied server in turn; Nginx uses round robin by default.

  2. IP Hash: This method calculates the hash based on the IP address of the requesting client, and then uses the hash value to model the total number of servers to get the assigned server address, if the client’s IP address is unchanged, then all its requests will be assigned to the same server.

  3. Least Connections: This approach assigns new requests to the server with the least number of connections based on statistics about the number of connections to the back-end servers, which ensures that the load on each back-end server is as balanced as possible.

  4. Weighted Round Robin: This approach allows higher-weighted servers to have more requests by assigning different weight values to each represented server.

  5. Weighted Least Connections: This approach combines the advantages of the first two load balancing methods, which not only takes into account the load on each server, but also allows higher weighted servers to have more connection opportunities.

  6. URL Hash: This is assigned based on the hash value of the access URL, and is usually used in cases where the back-end servers can’t share a session, allowing each request to access the previous data.

All of these methods can be configured through the upstream module of the Nginx configuration file. It is important to choose the appropriate load balancing method based on different request resources, backend server load, and business requirements.

III. Examples

3.1 1. polling (Round Robin)

Suppose we have three servers, Server A, Server B and Server C. In the Nginx configuration file, the configuration is as follows:

http {
    upstream backend {
        server serverA.example.com;
        server serverB.example.com;
        server serverC.example.com;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;
        }
    }
}

When a request arrives at the service, Nginx will distribute it to Server A, Server B, and Server C. Each cycle will distribute the request to each server in turn, which is how Round Robin load balancing is implemented.

3.2 2. IP Hash

Let’s assume that we have three servers, Server1 (IP address 192.168.1.1), Server2 (IP address 192.168.1.2) and Server3 (IP address 192.168.1.3), and that we want requests with the same client IP address to always access the same server. In the Nginx configuration file, we can configure it like this:

http {
    upstream backend {
        ip_hash;
        server server1.example.com;
        server server2.example.com;
        server server3.example.com;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;
        }
    }
}

The ip_hash directive is a built-in Nginx module directive that will use the client’s IP address hash to calculate a modulo the total number of servers the hash value will be assigned to, so that requests will be distributed to the server for which the hash was calculated.

3.3 Least Connections

In this approach, Nginx distributes requests to the server with the lowest number of connections so that the load on the back-end servers is as balanced as possible. In the Nginx configuration file, you can configure it like this:

http {
    upstream backend {
        least_conn;
        server server1.example.com;
        server server2.example.com;
        server server3.example.com;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;
        }
    }
}

In this example, the last_conn directive will enable the least-connected load balancing policy.

3.4 Weighted Round Robin (WR)

This approach can be done by configuring different weights for different servers so that higher weight servers have more requests. This can be configured in the Nginx configuration file like this:

http {
    upstream backend {
        server server1.example.com weight=3;
        server server2.example.com weight=2;
        server server3.example.com weight=1;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;
        }
    }
}

In this example, we give server1 a weight of 3, server2 a weight of 2, and server3 a weight of 1. What this really means is that 3/6 of the requests will be routed to Server1, 2/6 will be routed to Server2, and 1/6 will be routed to Server3.

3.5 Weighted Least Connections (WLC)

Unlike least-connected, weighted least-connected also takes into account the weight of each server and assigns new requests to the server with the fewest connections and the largest weight. In the Nginx configuration file, this can be configured like this:

http {
    upstream backend {
        least_conn;
        server server1.example.com weight=3;
        server server2.example.com weight=2;
        server server3.example.com weight=1;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;
        }
    }
}

In this example, the last_conn directive will enable the least-connected load balancing policy and set the weight of server1 to 3, server2 to 2, and server3 to 1. This will distribute new requests to the server with the lowest number of connections and the highest weight.

3.6 6. URL Hash

This approach calculates the hash value of the request URL to determine which backend server to route the request to. This is especially useful in cases where the back-end servers cannot share sessions, so that each request can access the previous data. In the Nginx configuration file, you can configure it like this:

http {
    upstream backend {
        hash $request_uri;
        server server1.example.com;
        server server2.example.com;
        server server3.example.com;
    }

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;
        }
    }
}

In this example, the hash directive is configured and $request_uri is used as the hashing algorithm to route requests to the server corresponding to the hash value. The routing result is fixed for each request, so it is guaranteed that requests for the same URL will be assigned to the same back-end server.

IV. TCP load

Nginx TCP load balancing is a technique for load balancing requests at the TCP level (such as FTP, SMTP, MySQL, and other application layer protocols). Similar to HTTP load balancing, Nginx TCP load balancing also distributes client requests to multiple back-end servers for load balancing purposes.

Nginx TCP load balancing can be divided into two types:

  1. Layer 4 Load Balancing: Layer 4 load balancing means that load balancing is done at the transport layer (Layer 4), where Nginx is only responsible for forwarding individual requests to the back-end servers and does not parse the content of the requests.

  2. Layer 7 Load Balancing: Layer 7 load balancing means that load balancing is done at the application layer (Layer 7), where Nginx has a higher level of request parsing and processing capabilities that enable more accurate load balancing, such as load balancing based on the URL of the request.

The following is an example of Nginx TCP load balancing:

stream {    
    upstream backend {    
        server server1.example.com:3306;
        server server2.example.com:3306;
        server server3.example.com:3306;
    }

    server {
        listen 3306;
        proxy_pass backend;
    }
}

In this example, we specify an upstream named backend that contains the addresses and port numbers of three backend servers. In the server segment, we define the port number to listen on as 3306 and route the requests to the backend upstream. In this way, client requests are forwarded equally to the three backend servers for load balancing purposes.

Note that in TCP load balancing, Nginx can only forward TCP requests, not UDP requests.