Code-Memo

Load balancing a commonly used technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault‑tolerant configurations. It involves distributing client requests across multiple servers to ensure no single server becomes a bottleneck.

Basic Configuration

To set up load balancing in Nginx, you’ll need to configure it in the nginx.conf file or within a specific server block. The configuration involves defining an upstream block and specifying how requests should be distributed.

http {
    upstream myapp {
        server backend1.example.com;
        server backend2.example.com;
        server backend3.example.com;
    }

    server {
        listen 80;

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

In this example:

Load Balancing Methods

Nginx supports several load balancing algorithms. You can configure these methods using the upstream directive.

Round Robin (Default)

upstream myapp {
    server backend1.example.com;
    server backend2.example.com;
}

Least Connections

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

IP Hash

upstream myapp {
    ip_hash;
    server backend1.example.com;
    server backend2.example.com;
}

Load Balancing with SSL/TLS

You can use Nginx to terminate SSL/TLS connections and then perform load balancing over HTTP. This setup involves configuring SSL/TLS in the server block and proxying requests to the backend servers.

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;

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

Quick Notes: