1 流量分流的A/B测试基本概念
A/B测试(也称为拆分测试或桶测试)是一种实验方法,主要是将客户端的请求按百分比分发到不同服务器上,用于比较两个或多个版本的某个变量,以确定哪个版本表现更好。这种方法通常应用于产品开发和数字营销领域。以下是A/B测试的关键要点:
目标设定:确定测试的目标,例如增加点击率、提高转化率或优化用户体验。
版本创建:创建不同版本的测试变量(例如网站页面、广告或按钮)。一个版本通常被称为“版本A”(控制组),另一个版本被称为“版本B”(实验组)。
用户分流:将用户随机分配到不同版本中,以确保测试结果的统计可靠性和公平性。通常会使用如上示例中的流量分配工具来完成这一步。
数据收集:在测试期间收集用户行为数据,如点击次数、页面停留时间和转化次数。
数据分析:对比各版本的性能,分析哪个版本在达到预定目标方面表现更佳。
实施改进:根据测试结果,实施性能更好的版本,从而优化产品或营销策略。
A/B测试的优点在于它可以基于真实用户行为提供量化的决策依据,从而提升产品和营销效果。
2 环境介绍与架构图
| 操作系统 | IP地址 | 主机名 | NGINX版本 | 角色 |
|---|---|---|---|---|
| Rocky 9.6 | 192.168.8.4 | loadbalance.luovip.cn | 1.26.2 | 负载均衡 |
| Rocky 9.6 | 192.168.8.5 | web1.luovip.cn | 1.26.2 | 普通业务服务器 |
| Rocky 9.6 | 192.168.8.6 | web2.luovip.cn | 1.26.2 | 普通业务服务器 |
loadbalance.luovip.cn是负责流量分配的NGINX服务器,它的IP地址是 192.168.8.4。流量通过NGINX分配到两个业务服务器:
1.web1.luovip.cn (IP: 192.168.8.5),接收20%的流量。
2.web2.luovip.cn (IP: 192.168.8.6),接收80%的流量。

3 准备hosts文件
这一步,需要在所有机器上完成准备,以便于大家都能用名称互相解析和访问
cat > /etc/hosts <<EOF
192.168.8.3 loadbalance.luovip.cn loadbalance
192.168.8.4 web1.luovip.cn web1
192.168.8.5 web2.luovip.cn web2
EOF
4 准备后端服务器
在web1和web2上安装并启动nginx服务之后,添加以下配置文件准备后端服务
# 配置文件修改参考 /etc/nginx/conf.d/default.conf
cat > /etc/nginx/conf.d/static.conf <<EOF
server {
listen 80 default_server;
location / {
root /usr/share/nginx/html;
# alias /usr/share/nginx/html;
index index.html index.htm;
}
}
EOF
# 让nginx重新加载配置文件生效
[root@web1 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web1 ~]# nginx -s reload
[root@web2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web2 ~]# nginx -s reload
在web1上创建页面:
[root@web1 ~]# echo web1 page > /usr/share/nginx/html/index.html
[root@web1 ~]# systemctl enable nginx --now
在web2上创建页面:
[root@web2 ~]# echo web2 page > /usr/share/nginx/html/index.html
[root@web2 ~]# systemctl enable nginx --now
5 配置NGINX分流配置文件
通过这个配置,NGINX会根据客户端IP地址,将流量按20%和80%的比例分配到两台业务服务器,实现A/B测试。
cat > /etc/nginx/conf.d/split.conf <<'EOF'
split_clients "${remote_addr}" $splittest {
20% "first";
80% "second";
* "";
}
upstream first {
server 192.168.8.5:80;
}
upstream second {
server 192.168.8.6:80;
}
server {
listen 80;
server_name loadbalance.luovip.cn;
location / {
proxy_pass http://$splittest$request_uri;
}
}
EOF
# 让nginx重新加载配置文件生效
[root@loadbalance ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@loadbalance ~]# nginx -s reload
这个配置文件用于在NGINX中设置A/B测试,具体如下:
1.split_clients模块:根据客户端IP地址($remote_addr)将流量分配到不同的上游组。在这个配置中,20%的流量分配到 first 组,80%的流量分配到 second 组。如果未匹配到分配规则,默认值为空("")。
2.upstream模块:定义了两个上游组,分别对应两台业务服务器:
first 组对应 `192.168.8.5:80``
second 组对应 192.168.8.6:80
3.server模块:定义了一个监听 80 端口的服务器,域名为 loadbalance.luovip.cn。当请求到达这个服务器时,将根据 split_clients 模块的结果,将请求转发到相应的上游组。
给nginx提供一个默认页用于和其他服务器区分:
[root@loadbalance ~]# echo default web site > /usr/share/nginx/html/index.html
[root@loadbalance ~]# systemctl enable nginx --now
[root@loadbalance ~]# systemctl restart nginx
6 测试分流效果
如果测试效果不好,主要是因为样本少的原因,可以手工修改配置文件中的比例,然后再测试
# 发现不同的主流去访问nginx的时候,已经分流成功了
[root@loadbalance ~]# curl http://loadbalance.luovip.cn
web2 page
[root@loadbalance ~]# curl http://loadbalance.luovip.cn
web1 page