YouTip LogoYouTip

Nginx Load Balancing

Introduction

Nginx can distribute traffic across multiple backend servers, improving availability and performance. Load balancing is critical for high-traffic applications.

Basic Load Balancing

upstream backend {
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
    server 192.168.1.12:8080;
}

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

Load Balancing Methods

# Round Robin (default)
upstream backend {
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
}

# Least Connections
upstream backend {
    least_conn;
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
}

# IP Hash (sticky sessions)
upstream backend {
    ip_hash;
    server 192.168.1.10:8080;
    server 192.168.1.11:8080;
}

Health Checks and Weights

upstream backend {
    server 192.168.1.10:8080 weight=3;
    server 192.168.1.11:8080 weight=1;
    server 192.168.1.12:8080 backup;
}

Summary

Use upstream blocks for load balancing. Choose round robin, least connections, or IP hash based on your needs. Set weights and backup servers for resilience.

← Nginx Performance TuningNginx SSL Configuration β†’