Hi!请登陆

nginx模块之split_clients

2021-5-23 65 5/23

split_clients模块默认已经编译进Nginx , 通过–without-http_split_clients_module 禁用。

功能:
1、基于已有变量创建新变量,可为实现AB测试提供更多的可能性。
2、对已有变量的值执行MurmurHash2算法得到32位整型哈希数字,记为hash。
3、32位无符号整型的最大数字2^32-1,记为max。
4、哈希数字与最大数字相除hash/max,可以得到百分比percent。
5、配置指令中只是了各个百分比构成的范围,如0-1%,1%-5%等,及范围对应的值。
6、当percent落在哪个范围里,新变量的值就对应着其后的参数。

已有变量
1、字符串
2、一个或多个变量
3、变量与字符串的组合

case规则
1、xx.xx% ,支持小数点后2位,所有项的百分比相加不能超过100% 。
2、* ,由它匹配剩余的百分比(100%减去以上所有相加的百分比)。

split_clients指令

Syntax: split_clients string $variable { ... }
Default: ---
Context: http

 

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 upstream {
        server 192.168.1.10:8080 max_fails=1 fail_timeout=1m;
    }

    #可是怎么让请求均匀的分配到这三块盘上呢,一般是配置split_clients方法,方法如下:
    split_clients $request_uri $disk {
        33% disk1;
 		33% disk2;
 		* disk3;
    }
    #nginx做缓存的时候,可能用到多快盘。比如如下配置:
    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  localhost;

        #之后在location中配置如下:
        location ~ (amber\.um38)$ {
            proxy_pass http://upstream;
            add_header Nginx-Cache "$upstream_cache_status";
            proxy_cache_valid 10s;
            proxy_cache_key $uri;
            proxy_cache cache_$disk;
        }
    }
}

 

Tag:

相关推荐