Sobes.tech
Junior — Middle

What methods are used for load balancing in nginx?

sobes.tech AI

Answer from AI

In nginx, the main methods used for load balancing between servers (upstream) are:

  1. Round Robin (default) — requests are sequentially directed to each server in turn.

  2. Least Connections — requests are directed to the server with the fewest active connections, which helps balance the load when request durations vary.

  3. IP Hash — requests are distributed to servers based on a hash of the client's IP, ensuring the client is bound to a single server (session persistence).

Example configuration:

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

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

There are also additional features, such as server weight (weight), backup servers (backup), but the main methods are those listed above.

What methods are used for load balancing in nginx… - sobes.tech