0
0
Nginxdevops~5 mins

Least connections in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you have multiple servers handling requests, you want to send new requests to the server with the fewest active connections. This helps balance the load evenly and keeps your app fast and reliable.
When you run a website on several servers and want to avoid overloading any single server.
When some servers are slower and you want to send fewer requests to busy servers.
When you want to improve user experience by reducing wait times during high traffic.
When you want to automatically distribute traffic without manual intervention.
When you want a simple way to balance load without complex health checks.
Config File - nginx.conf
nginx.conf
http {
    upstream myapp {
        least_conn;
        server 192.168.1.101:80;
        server 192.168.1.102:80;
        server 192.168.1.103:80;
    }

    server {
        listen 80;

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

This configuration sets up a group of servers named myapp. The least_conn directive tells Nginx to send new requests to the server with the fewest active connections. The server blocks list the backend servers by IP and port. The proxy_pass forwards incoming requests to this group.

Commands
Check the Nginx configuration file for syntax errors before applying changes.
Terminal
nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload Nginx to apply the new configuration without stopping the service.
Terminal
systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Send a simple request to the Nginx server to verify it is forwarding requests correctly.
Terminal
curl -I http://localhost
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Thu, 01 Jun 2024 12:00:00 GMT Content-Type: text/html Content-Length: 612 Connection: keep-alive
Key Concept

If you remember nothing else from this pattern, remember: least connections sends new requests to the backend server with the fewest active connections to balance load evenly.

Common Mistakes
Forgetting to include the least_conn directive inside the upstream block.
Without least_conn, Nginx uses the default round-robin method, ignoring connection counts.
Always add least_conn inside the upstream block to enable this load balancing method.
Not testing the configuration with nginx -t before reloading.
Syntax errors can cause Nginx to fail to reload, leading to downtime.
Run nginx -t to verify configuration syntax before reloading.
Reloading Nginx without proper permissions or using the wrong command.
Reload may fail silently or cause service interruption.
Use sudo systemctl reload nginx or run as root to reload safely.
Summary
Configure the upstream block with least_conn to balance requests by active connections.
Test the configuration syntax with nginx -t before applying changes.
Reload Nginx to apply the new load balancing method without downtime.
Verify the setup by sending requests and checking responses.