This article describes how to configure easy A/B testing in Nginx.

Background Prerequisites

Sometimes we need to do simple A/B tests that don’t require complex conditions, so we can use Nginx’s ngx_http_split_clients_module module.

Install the ngx_http_split_clients_module module

Generally this module already comes with it, if not it is recommended to install our packaged N.WTF

Configuring Nginx

For example, we want 20% of our users to be forwarded to the URL https://example.com/, 30% to the URL https://example.org/, and the rest to the URL https://examle.edu/.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
split_clients "${remote_addr}AAA" $variant {
    20%               https://example.com/;
    30%               https://example.org/;
    *                 https://example.edu/;
}

server {
    listen 80;
    listen [::]:80;
    server_name _;

    return 302 ${variant};
}

In the above example, the IP address plus the AAA string requested by the visitor is converted to a number using MurmurHash2, and if the resulting number is in the first 20%, then the $ variant value is https://example.com/, and the corresponding value in the middle 30% interval is https://example.org/, and the rest is https://example.edu/.

Then we find two machines with different IPs and test them.

Machine A.

1
2
3
4
5
6
7
8
root@debian ~ # curl -I 192.0.2.2
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Sat, 02 Jul 2022 20:51:43 GMT
Content-Type: text/html
Content-Length: 138
Connection: keep-alive
Location: https://example.com/

Machine B.

1
2
3
4
5
6
7
8
root@debian ~ #  curl -I 192.0.2.2                         
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Sat, 02 Jul 2022 20:52:12 GMT
Content-Type: text/html
Content-Length: 138
Connection: keep-alive
Location: https://example.org/

Then there can be more flexible uses, such as specifying different directories.

1
root /var/www/${variant};

Specify a different home page.

1
index index-${variant}.html;

This relatively simple A/B test is now complete ~