Configure Weighted Round-Robin Load Balancing in Nginx
📖 Scenario: You are setting up a simple load balancer using Nginx to distribute web traffic across multiple backend servers. Some servers are more powerful, so they should receive more traffic. You will configure weighted round-robin to achieve this.
🎯 Goal: Build an Nginx configuration that uses weighted round-robin to balance requests between three backend servers with different weights.
📋 What You'll Learn
Create an upstream block named backend with three servers
Assign weights 3, 2, and 1 to the servers respectively
Configure the server block to proxy requests to the backend upstream
Print the final Nginx configuration to verify the setup
💡 Why This Matters
🌍 Real World
Weighted round-robin helps balance load efficiently when backend servers have different capacities, improving website performance and reliability.
💼 Career
Understanding Nginx load balancing is essential for DevOps roles managing scalable web applications and infrastructure.
Progress0 / 4 steps
1
Create the upstream block with backend servers
Create an upstream block named backend with these three servers exactly: server1.example.com, server2.example.com, and server3.example.com. Do not add weights yet.
Nginx
Hint
Use the upstream directive and list each server inside curly braces.
2
Add weights to the backend servers
Add weights to the servers inside the upstream backend block: assign weight 3 to server1.example.com, weight 2 to server2.example.com, and weight 1 to server3.example.com. Use the syntax server hostname weight=number;.
Nginx
Hint
Add weight= followed by the number after each server hostname.
3
Configure the server block to use the upstream backend
Create a server block that listens on port 80 and proxies all requests to the backend upstream using proxy_pass http://backend; inside the location / block.
Nginx
Hint
Use server block with listen 80; and inside location / use proxy_pass http://backend;.
4
Print the final Nginx configuration
Print the entire Nginx configuration to verify the weighted round-robin setup. Use print() to output the configuration as a string exactly as written.
Nginx
Hint
Use print() with the full configuration string including newlines and indentation.
Practice
(1/5)
1. What does the weight parameter control in nginx's weighted round-robin load balancing?
easy
A. The maximum number of connections a server can handle
B. The timeout duration for server responses
C. The order in which servers are listed in the config
D. The number of requests sent to a server relative to others
Solution
Step 1: Understand the role of weight in load balancing
The weight defines how many requests a server receives compared to others.
Step 2: Relate weight to request distribution
Higher weight means more requests go to that server, balancing load by capacity.
Final Answer:
The number of requests sent to a server relative to others -> Option D
Quick Check:
Weight controls request share [OK]
Hint: Weight sets request share per server [OK]
Common Mistakes:
Confusing weight with timeout
Thinking weight limits connections
Assuming weight sets server order
2. Which of the following is the correct syntax to assign weights to servers in nginx for weighted round-robin?
easy
A. server backend1.example.com weight:3;
B. server backend1.example.com weight 3;
C. server backend1.example.com weight=3;
D. server backend1.example.com weight(3);
Solution
Step 1: Recall nginx upstream server syntax
Weights are assigned using weight=number with an equal sign and semicolon.
Step 2: Check each option's syntax
Only server backend1.example.com weight=3; uses the correct weight=3; format.
Final Answer:
server backend1.example.com weight=3; -> Option C
Quick Check:
Correct syntax uses equal sign and semicolon [OK]
Hint: Use weight=number; syntax for servers [OK]
Common Mistakes:
Omitting equal sign
Using colon instead of equal sign
Using parentheses incorrectly
3. Given this nginx upstream block:
upstream backend {
server backend1.example.com weight=3;
server backend2.example.com weight=1;
}
If 8 requests arrive, how many requests will backend2 receive approximately?
medium
A. 2
B. 6
C. 4
D. 1
Solution
Step 1: Calculate total weight
backend1 weight=3, backend2 weight=1, total = 4.
Step 2: Calculate requests for backend2
Requests for backend2 = (1/4) * 8 = 2.
Final Answer:
2 -> Option A
Quick Check:
Requests proportional to weight [OK]
Hint: Requests split by weight ratio [OK]
Common Mistakes:
Ignoring total weight sum
Assigning equal requests despite weights
Miscounting total requests
4. You configured nginx with weighted round-robin but all servers receive equal requests. What is the most likely cause?
medium
A. nginx does not support weighted round-robin
B. Weights were not specified correctly in the config
C. The servers have identical IP addresses
D. The upstream block is missing the weight directive
If weights are missing or incorrect, nginx defaults to equal distribution.
Step 2: Analyze the options
Weights were not specified correctly in the config states weights not specified correctly, which explains equal requests.
Final Answer:
Weights were not specified correctly in the config -> Option B
Quick Check:
Incorrect weights cause equal load [OK]
Hint: Check weight syntax if load is equal [OK]
Common Mistakes:
Assuming nginx lacks weighted support
Confusing IP issues with load balancing
Missing weight directive entirely
5. You have three servers with weights 5, 3, and 2. You want to add a fourth server but keep the total weight sum at 10. What weight should you assign to the new server to maintain the same total weight and keep the relative proportions of the first three servers?
hard
A. 0 (do not add the server)
B. 5
C. 2
D. 1
Solution
Step 1: Calculate current total weight
Sum of existing weights = 5 + 3 + 2 = 10.
Step 2: Determine weight for new server
To keep total sum at 10 when adding a fourth server, assign weight=0 to it.
Step 3: Verify proportions
Weight=0 means no requests to the new server, so first three retain exact proportions (5:3:2 out of total 10).
Final Answer:
0 (do not add the server) -> Option A
Quick Check:
Weight=0 keeps total and proportions unchanged [OK]
Hint: Assign weight=0 to keep total sum and proportions [OK]
Common Mistakes:
Assigning positive weight that increases total sum
Forgetting to maintain exact total of 10
Ignoring impact on relative proportions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.