0
0
Nginxdevops~5 mins

A/B testing with split_clients in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
A/B testing helps you show different versions of a website to different users to see which works better. The split_clients directive in Nginx lets you divide users into groups automatically based on their IP or other data, so you can test different content easily.
When you want to test two different webpage designs to see which users like more.
When you want to show a new feature to only some users without affecting everyone.
When you want to compare two different backend servers by sending users to each one randomly.
When you want to gradually roll out a change by increasing the percentage of users seeing it.
When you want to collect data on user behavior for different versions of your site.
Config File - nginx.conf
nginx.conf
http {
    split_clients "$remote_addr" $variant {
        50%     "A";
        *       "B";
    }

    server {
        listen 80;

        location / {
            if ($variant = "A") {
                return 200 "You are in group A";
            }
            if ($variant = "B") {
                return 200 "You are in group B";
            }
        }
    }
}

The split_clients directive divides users into groups based on their IP address ($remote_addr).

Here, 50% of users get the variant "A" and the rest get "B".

Inside the server block, requests are checked for their group and respond with a message accordingly.

Commands
This command tests the Nginx configuration file for syntax errors before applying it.
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
This command reloads Nginx to apply the new configuration without stopping the server.
Terminal
systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command sends a request to the local Nginx server to see which variant the user is assigned to.
Terminal
curl -s http://localhost/
Expected OutputExpected
You are in group A
-s - Runs curl silently without progress output
Run the request multiple times to observe that about half the requests return group A and half return group B.
Terminal
curl -s http://localhost/
Expected OutputExpected
You are in group B
-s - Runs curl silently without progress output
Key Concept

If you remember nothing else from this pattern, remember: split_clients divides users into groups based on a key like IP, letting you serve different content for A/B testing.

Common Mistakes
Using split_clients without a proper key like $remote_addr
Without a stable key, users may be assigned randomly on each request, breaking consistent A/B testing.
Always use a consistent key such as $remote_addr or a cookie to keep users in the same group.
Not reloading Nginx after changing the configuration
Changes won't take effect until Nginx reloads, so the test won't work as expected.
Run 'nginx -t' to check config, then 'systemctl reload nginx' to apply changes.
Summary
Use split_clients in Nginx to split users into groups based on their IP address.
Test the configuration syntax with 'nginx -t' before reloading Nginx.
Reload Nginx to apply changes and verify by sending requests to see which group users belong to.