📖 Scenario: You are working as a web server administrator. Your website wants to test two different versions of a landing page to see which one users like better. This is called A/B testing.You will use Nginx's split_clients module to send 50% of visitors to version A and 50% to version B.
🎯 Goal: Build an Nginx configuration that uses split_clients to assign visitors to either variant_a or variant_b evenly based on their IP address.This will help the website show different content to different users for testing.
📋 What You'll Learn
Create a variable called $variant using split_clients
Split traffic 50% to variant_a and 50% to variant_b
Use $remote_addr as the key for splitting
Add a server block listening on port 8080
Return the assigned variant in the response body
💡 Why This Matters
🌍 Real World
Websites often want to test different versions of pages to improve user experience. Using Nginx's split_clients lets you do this easily at the server level.
💼 Career
DevOps engineers and site reliability engineers use Nginx configurations like this to manage traffic and run experiments without changing application code.
Progress0 / 4 steps
1
Create basic Nginx server block
Create an Nginx server block that listens on port 8080 and returns the text "Hello from Nginx" for any request.
Nginx
Hint
Use return 200 "Hello from Nginx"; inside the location / block.
2
Add split_clients block to define $variant
Add a split_clients block inside the server block that creates a variable called $variant. Use $remote_addr as the key. Split traffic 50% to variant_a and 50% to variant_b.
Nginx
Hint
Use split_clients "$remote_addr" $variant { 50% variant_a; * variant_b; } inside the server block.
3
Modify location block to return the variant
Change the location / block to return the text "User assigned to: $variant" using the return directive.
Nginx
Hint
Use return 200 "User assigned to: $variant"; inside the location / block.
4
Test the configuration output
Run the Nginx server and send a request to http://localhost:8080/. The response should be either User assigned to: variant_a or User assigned to: variant_b depending on your IP.
Nginx
Hint
Use curl http://localhost:8080/ to test the response.
Practice
(1/5)
1. What is the main purpose of the split_clients directive in nginx for A/B testing?
easy
A. To redirect all traffic to a single URL
B. To cache static files for faster loading
C. To compress responses before sending to clients
D. To divide visitors into groups based on a variable for testing different versions
Solution
Step 1: Understand the role of split_clients
The split_clients directive is designed to split traffic into groups based on a variable like IP or cookie.
Step 2: Connect to A/B testing purpose
This splitting allows serving different site versions to different groups, which is the core of A/B testing.
Final Answer:
To divide visitors into groups based on a variable for testing different versions -> Option D
Quick Check:
split_clients divides visitors for testing [OK]
Hint: Remember: split_clients splits visitors into groups [OK]
Common Mistakes:
Confusing split_clients with caching directives
Thinking it redirects all traffic to one URL
Assuming it compresses responses
2. Which of the following is the correct syntax to use split_clients in nginx to split traffic by cookie $cookie_user into two groups: 50% 'A' and 50% 'B'?
easy
A. split_clients $cookie_user $variant { 50% A; 50% B; }
B. split_clients $cookie_user { 50% A; 50% B; }
C. split_clients $cookie_user $variant { 50 A; 50 B; }
D. split_clients $cookie_user $variant { 50% "A"; 50% "B"; }
Solution
Step 1: Review split_clients syntax
The correct syntax requires specifying the source variable, the destination variable, and the percentage groups with semicolons.
split_clients $remote_addr $group {
30% A;
70% B;
}
server {
location / {
if ($group = A) {
return 200 'Group A content';
}
return 200 'Group B content';
}
}
What response do 70% of visitors receive?
medium
A. Group A content
B. Error: invalid syntax
C. Group B content
D. No response, connection closed
Solution
Step 1: Understand split_clients distribution
The split_clients directive splits visitors by IP into 30% group A and 70% group B.
Step 2: Identify the majority group
The configuration assigns 70% of visitors to group B based on the hash distribution, so they receive 'Group B content'.
Final Answer:
Group B content -> Option C
Quick Check:
70% of visitors get Group B content [OK]
Hint: Larger split_clients percentage gets more visitors [OK]
Common Mistakes:
Assuming visitors always get Group A content
Thinking syntax error occurs
Misunderstanding the traffic split percentages
4. You wrote this nginx config:
split_clients $cookie_user $group {
50% A
50% B
}
But nginx fails to start. What is the error?
medium
A. Percentages must add up to 1000%
B. Missing semicolons after percentage groups
C. Destination variable $group is not declared
D. Using $cookie_user instead of $remote_addr
Solution
Step 1: Check syntax for split_clients block
Each percentage group line must end with a semicolon (;). The config misses semicolons after '50% A' and '50% B'.
Step 2: Validate other options
Using $cookie_user is valid. $group is declared as destination variable. Percentages add to 100%, not 1000%.
Final Answer:
Missing semicolons after percentage groups -> Option B
Quick Check:
Each group line ends with ; [OK]
Hint: Always end split_clients group lines with semicolons [OK]
Common Mistakes:
Forgetting semicolons after group percentages
Thinking variable names must be IP only
Misreading percentage sums
5. You want to run an A/B test with three groups: 20% 'A', 30% 'B', and 50% 'C'. You also want to ensure users stay in the same group on repeat visits using a cookie $cookie_user. Which nginx config snippet correctly implements this?
hard
A. split_clients $cookie_user $group {
20% A;
30% B;
50% C;
}
server {
location / {
if ($group = A) { return 200 'A version'; }
if ($group = B) { return 200 'B version'; }
return 200 'C version';
}
}
B. split_clients $remote_addr $group {
20% A;
30% B;
50% C;
}
server {
location / {
if ($group = A) { return 200 'A version'; }
if ($group = B) { return 200 'B version'; }
return 200 'C version';
}
}
C. split_clients $cookie_user $group {
20 A;
30 B;
50 C;
}
server {
location / {
if ($group = A) { return 200 'A version'; }
if ($group = B) { return 200 'B version'; }
return 200 'C version';
}
}
D. split_clients $cookie_user $group {
20% A
30% B
50% C
}
server {
location / {
if ($group = A) { return 200 'A version'; }
if ($group = B) { return 200 'B version'; }
return 200 'C version';
}
}
Solution
Step 1: Confirm correct split_clients syntax and variable
split_clients $cookie_user $group {
20% A;
30% B;
50% C;
}
server {
location / {
if ($group = A) { return 200 'A version'; }
if ($group = B) { return 200 'B version'; }
return 200 'C version';
}
} uses $cookie_user to keep users in the same group on repeat visits and correctly uses percentages with semicolons.
Step 2: Check other options for errors
split_clients $remote_addr $group {
20% A;
30% B;
50% C;
}
server {
location / {
if ($group = A) { return 200 'A version'; }
if ($group = B) { return 200 'B version'; }
return 200 'C version';
}
} uses $remote_addr which may change and not guarantee consistent grouping. split_clients $cookie_user $group {
20 A;
30 B;
50 C;
}
server {
location / {
if ($group = A) { return 200 'A version'; }
if ($group = B) { return 200 'B version'; }
return 200 'C version';
}
} misses percent signs. split_clients $cookie_user $group {
20% A
30% B
50% C
}
server {
location / {
if ($group = A) { return 200 'A version'; }
if ($group = B) { return 200 'B version'; }
return 200 'C version';
}
} misses semicolons after group lines.
Final Answer:
split_clients $cookie_user $group {
20% A;
30% B;
50% C;
}
server {
location / {
if ($group = A) { return 200 'A version'; }
if ($group = B) { return 200 'B version'; }
return 200 'C version';
}
} -> Option A
Quick Check:
Use cookie variable and proper syntax for consistent groups [OK]
Hint: Use cookie variable and semicolons for stable multi-group split [OK]
Common Mistakes:
Using IP instead of cookie for consistent user grouping
Omitting percent signs or semicolons
Misordering or missing group percentages
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.