split_client模块
split_client该模块和map模块相似也是通过生成新的变量来完成AB测试的split_clients模块,这个模块可以根据变量的值按照百分比的方式生成新的变量。
什么是AB测:比如我们的产品不太确定推出的功能用户是否接受,所以推出了多个版本的功能让某一个百分比的用户去尝试某一类功能,最后看大家反馈来决定最终使用哪个功能版本。split_client模块就是根据某类变量的值按照百分比生成新变量的值。
ngx_http_split_clients_module
模块创建适合于A/B测试(也叫做分离测试)的变量。或者当做负载均衡来使用。我相信,有很多地方可以用上此模块。
split_clients指令
语法:split_clients string $variable { ... }
默认值: —
配置段: http
配置实例
例一:
http{
split_clients "${remote_addr}AAA" $variant{
0.51% .one;
20.0% .two;
50.5% .three;
* "";
}
server{
location /{
index index${variant}.html
}
}
}
IP地址加上AAA字符串会使用MurmurHash2转换成数字。得出的数字在前0.5%,那么$variant值为.one,相应的在0.5-2.0%区间的值为.two,其他的为空字符。
原始字符串的值经过MurmurHash2算法进行了哈希。 示例中,哈希值从0到21474835(0.5%)对应于变量$varian
的".one"
值, 哈希值从21474836到107374180(2%)对应于值".two"
, 哈希值从107374181到4294967295对应于值""
(一个空字符串)。
例二:转载例子
nginx做缓存的时候,可能用到多快盘。比如如下配置:
proxy_cache_path /disk1 levels=1:2 keys_zone=cache_disk1:10m max_size=5g inactive=7d use_temp_path=off;
proxy_cache_path /disk2 levels=1:2 keys_zone=cache_disk2:10m max_size=5g inactive=7d use_temp_path=off;
proxy_cache_path /disk3 levels=1:2 keys_zone=cache_disk3:10m max_size=5g inactive=7d use_temp_path=off;
可是怎么让请求均匀的分配到这三块盘上呢,一般是配置split_clients方法,方法如下:
split_clients $request_uri $disk {
33% disk1;
33% disk2;
* disk3;
}
之后在location中配置如下:
location ~ (live\.m3u8)$ {
proxy_pass http://liveUpstream;
proxy_cache_valid 10s;
proxy_ignore_headers cache-control;
proxy_cache_key $uri;
proxy_cache cache_$disk;
}
由location的标红配置可以看出,该缓存磁盘调用的是split_clients函数,会按照百分比获取一个磁盘。完整配置如下:
user root;
worker_processes 1;
events {
worker_connections 10240;
use epoll;
}
worker_rlimit_nofile 30000;
http {
lua_shared_dict nginxcache 60m;
include mime.types;
default_type application/octet-stream;
log_format session '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'upstream_cache_status=$upstream_cache_status '
'"$http_user_agent" "$http_x_forwarded_for" "request_time
$request_time"';
access_log logs/access.log session;
error_log logs/error.log error;
sendfile on;
keepalive_timeout 65;
client_max_body_size 2m;
client_body_buffer_size 512k;
proxy_cache_revalidate on;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_cache_lock on;
proxy_cache_lock_timeout 6s;
proxy_cache_lock_age 6s;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
proxy_temp_path ../proxy_temp;
upstream vodUpstream {
server 10.0.224.103:8080 max_fails=1 fail_timeout=1m;
}
split_clients $request_uri $disk {
33% disk1;
33% disk2;
* disk3;
}
proxy_cache_path /disk1 levels=1:2 keys_zone=cache_disk1:600m max_size=10g inactive=7d use_temp_path=off;
proxy_cache_path /disk2 levels=1:2 keys_zone=cache_disk2:600m max_size=10g inactive=7d use_temp_path=off;
proxy_cache_path /disk3 levels=1:2 keys_zone=cache_disk3:600m max_size=10g inactive=7d use_temp_path=off;
server {
listen 8080;
server_name starStreamerManager;
location ~ (live\.m3u8)$ {
proxy_pass http://vodUpstream;
add_header Nginx-Cache "$upstream_cache_status";
proxy_cache_valid 10s;
proxy_cache_key $uri;
proxy_cache cache_$disk;
}
}
}