# underscores_in_headers on;
#当使用超过一个map时,下面两个参数需要调大,否则会报错
map_hash_max_size 262144;
map_hash_bucket_size 262144;
#定义3个upstream用于AB测试,用于显示不同的内容,相应下面有建3个虚拟主机端口与此对应
upstream def {
server 127.0.0.1:8080;
}
upstream a {
server 127.0.0.1:8081;
}
upstream b {
server 127.0.0.1:8082;
}
# 定义map,$cookie_aid是指从cookie内取aid的值,当cookie内aid的值为"12345"时,$upserver为a,$upserver我这边是用的是upstrem的名称,其它类似
map $cookie_aid $upserver {
default "def";
"12345" "a";
"67890" "b";
}
# 从$http_pid内取值,是从http header内取pid的的值,当http header内pid的值为"12345"时,$headerpid 为a,其它类似
map $http_pid $headerpid {
default "def";
"12345" "a";
"67890" "b";
}
# 从$host内取值,$host对应的是用户访问的域名,当域名的值为12345.abtest-b.xxxxx.com时,$bserver 为a,其它类似
map $host $bserver {
12345.abtest-b.xxxxx.com "a";
67890.abtest-b.xxxxx.com "b";
.abtest-b.xxxxx.com "def";
default "def";
}
# 从$arg_userid内取值,$arg_userid对应的是用户访问的URL上所带的参数userid的值,当userid的值为12345时,$userid为a,其它类似
map $arg_userid $userid {
default "def";
"12345" "a";
"67890" "b";
}
server {
listen 80;
server_name abtest.xxxxx.com;
charset utf-8;
root /data/www/a/;
access_log /data/log/nginx/abtest.internal.weimobdev.com_access.log main;
error_log /data/log/nginx/abtest.internal.weimobdev.com_error.log info;
location / {
# 通过以下的if语句,实现多个变量的判断,当cookie内取aid的值判断完后,再判断http header内pid的值
if ( $upserver = "def" ) {
set $upserver $headerpid;
}
# 由于$upserver我这边是用的是upstrem的名称,所以可以直接转发
proxy_pass http://$upserver;
}
}
server {
listen 80;
server_name abtest-b.xxxxx.com *.abtest-b.xxxxx.com;
charset utf-8;
root /data/www/a/;
access_log /data/log/nginx/abtest-b.xxxxx.com_access.log main;
error_log /data/log/nginx/abtest-b.xxxxx.com_error.log info;
location / {
# 通过以下的if语句,实现多个变量的判断,当$host内的域名判断完后,再判断URL上所带的参数userid的值
if ( $bserver = "def" ) {
set $bserver $userid;
}
#
proxy_pass http://$bserver;
}
}
server {
listen 8080;
charset utf-8;
root /data/www/default/;
access_log /data/log/nginx/default.log;
}
server {
listen 8081;
charset utf-8;
root /data/www/a/;
access_log /data/log/nginx/a.log;
}
server {
listen 8082;
charset utf-8;
root /data/www/b/;
access_log /data/log/nginx/b.log;
}