0% found this document useful (0 votes)
15 views5 pages

Nginx Supports Several Types of Load Balancing Methods To Distribute Traffic Across Multiple Backend Servers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Nginx Supports Several Types of Load Balancing Methods To Distribute Traffic Across Multiple Backend Servers

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Nginx supports several types of load balancing methods to distribute traffic across multiple backend

servers. Here are the primary types and how to configure each:

1. Round Robin

Round Robin is the default load balancing method in Nginx. It distributes client requests evenly across
the backend servers in the order they are listed.

Configuration:

http {

upstream backend {

server backend1.example.com;

server backend2.example.com;

server backend3.example.com;

server {

listen 80;

location / {

proxy_pass http://backend;

2. Least Connections

This method passes each request to the server with the least number of active connections.

Configuration:

http {

upstream backend {

least_conn;
server backend1.example.com;

server backend2.example.com;

server backend3.example.com;

server {

listen 80;

location / {

proxy_pass http://backend;

3. IP Hash

The IP Hash method uses the client’s IP address to determine which server will handle the request. This
ensures that a specific client always connects to the same server.

Configuration:

http {

upstream backend {

ip_hash;

server backend1.example.com;

server backend2.example.com;

server backend3.example.com;

server {

listen 80;

location / {

proxy_pass http://backend;

}
}

4. Generic Hash

This method distributes requests based on a user-defined key using the hash directive. It ensures
requests with the same key go to the same server.

Configuration:

http {

upstream backend {

hash $request_uri;

server backend1.example.com;

server backend2.example.com;

server backend3.example.com;

server {

listen 80;

location / {

proxy_pass http://backend;

5. Least Time

This method distributes requests to the server with the lowest average response time and least number
of active connections. It requires the stream module.

Configuration:

stream {

upstream backend {

least_time connect;

server backend1.example.com;
server backend2.example.com;

server backend3.example.com;

server {

listen 80;

proxy_pass backend;

Advanced Features:

Health Checks:

To ensure that Nginx only forwards requests to healthy servers, you can configure active health checks.

Configuration:

http {

upstream backend {

server backend1.example.com;

server backend2.example.com;

server backend3.example.com;

health_check interval=10s fails=3 passes=2;

server {

listen 80;

location / {

proxy_pass http://backend;

}
}

Sticky Sessions:

Sticky sessions ensure that requests from the same client are always directed to the same backend
server.

Configuration:

http {

upstream backend {

sticky;

server backend1.example.com;

server backend2.example.com;

server backend3.example.com;

server {

listen 80;

location / {

proxy_pass http://backend;

Summary:

• Round Robin: Distributes requests evenly across servers.

• Least Connections: Directs traffic to the server with the fewest active connections.

• IP Hash: Ensures requests from the same client IP always go to the same server.

• Generic Hash: Distributes requests based on a custom key.

• Least Time: Uses response time to distribute requests to the fastest server.

You might also like