Beginner Fundamentals

Load Balancing

When one server is not enough, Nginx can spread requests across several backends. This is load balancing, and it is built on the upstream block.

Defining an Upstream

An upstream block lists the backend servers and lives in the http context:

http {
    upstream app_servers {
        server 127.0.0.1:3001;
        server 127.0.0.1:3002;
        server 127.0.0.1:3003;
    }
}

Using the Upstream

Point proxy_pass at the upstream by name:

server {
    listen 80;
    location / {
        proxy_pass http://app_servers;
    }
}

Nginx now distributes incoming requests among the three backends.

Balancing Methods

By default Nginx uses round-robin, sending each new request to the next server in turn. You can choose other strategies:

upstream app_servers {
    least_conn;
    server 127.0.0.1:3001;
    server 127.0.0.1:3002;
}
  • round-robin (default): even rotation across servers.
  • least_conn: send to the server with the fewest active connections.
  • ip_hash: route each client IP to the same backend, useful for sessions.

Weights and Backups

upstream app_servers {
    server 127.0.0.1:3001 weight=3;
    server 127.0.0.1:3002;
    server 127.0.0.1:3003 backup;
}

A higher weight gets more traffic. A backup server is used only when the others are down.