0
0
Nginxdevops~5 mins

Weighted round-robin in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you have multiple servers to handle requests, you want to share the work fairly. Weighted round-robin lets you send more requests to stronger servers and fewer to weaker ones. This helps balance the load based on each server's capacity.
When you run a website on several servers with different power levels and want to use them efficiently.
When some servers are faster or have more resources and should get more traffic.
When you want to avoid overloading a small server while others are idle.
When you want simple load balancing without complex setup.
When you want to improve response times by using stronger servers more.
Config File - nginx.conf
nginx.conf
http {
    upstream myapp {
        server server1.example.com weight=3;
        server server2.example.com weight=1;
        server server3.example.com weight=2;
    }

    server {
        listen 80;

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

The upstream block defines a group of backend servers named myapp. Each server line includes a weight that tells NGINX how many requests to send relative to others. For example, server1 gets 3 times more requests than server2.

The server block listens on port 80 and forwards incoming requests to the myapp group using the weighted round-robin method.

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 weighted round-robin configuration without stopping the server.
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 to backend servers.
Terminal
curl -I http://localhost
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Wed, 01 Jan 2025 12:00:00 GMT Content-Type: text/html Content-Length: 612 Connection: keep-alive
Key Concept

Weighted round-robin lets you send more requests to stronger servers by assigning them higher weights in the load balancer.

Common Mistakes
Not setting weights on servers and expecting weighted behavior.
Without weights, NGINX uses simple round-robin, treating all servers equally.
Add the weight parameter to each server line to control traffic distribution.
Forgetting to test the configuration with 'nginx -t' before reloading.
Syntax errors can cause NGINX to fail to reload or crash.
Always run 'nginx -t' to verify configuration syntax before reloading.
Reloading NGINX without applying the new config file.
Reloading applies the current config; changes won't take effect if not saved.
Save the config file with weights before running 'systemctl reload nginx'.
Summary
Define backend servers with weights in the 'upstream' block to control request distribution.
Test the NGINX configuration syntax with 'nginx -t' before applying changes.
Reload NGINX to apply the weighted round-robin load balancing without downtime.