配置过程:
1.根据cookies特征值实现
在做的过程中把login的操作给做了过滤。登录过程将cookie中设置了一个新的cookie值 SESSIONID =gray这样在nginx配置中
加入筛选
Root不要配置在最前面而是配置在location中
#过滤login 请求进来会优先判断 请求是不是/ gina-client/login 然后判断是不是 / gina-client/
location /client/login {
proxy_pass http://client_stable_gray;
}
登陆过程加上cookie
location /client/{
set $group "client_stable";
if ( $http_cookie ~* "SESSIONID = gray" ) {
set $group "client_stable_gray";
}
proxy_pass http://$group;
}
筛选root
location / {
set root /data0/project/gina/html/agent;
if ( $http_cookie ~*"SESSIONID = gray" ) {
set root /data0/project/gina/html/agent_gray/magneto/dist/agent;
}
}
2.根据cookie ||||| lua方式
如果单独抽出来写lua脚本
cookies_gray.lua
local uin = ngx.var.cookie_loginuin
--取cookies里的loginuin字段,末尾被2整数的灰度
if uin ~= nil and string.sub(uin,string.len(uin))%2 == 0then
ngx.exec("@gray_env")
else
ngx.exec("@product_env")
end
nginx.conf
...
#http
lua_code_cacheoff;#正式上线记得打开cache
lua_package_path"/usr/local/openresty/lualib/?.lua;;";
lua_shared_dictconfig 1m;
...
#server
location / {
#access_by_lua_file conf/lua/ip_gray.lua;
access_by_lua_file conf/lua/cookies_gray.lua;
}
location@gray_env {
proxy_passhttp://gray_env;
proxy_set_header Host $http_host;
}
location@product_env {
proxy_passhttp://product_env;
proxy_set_header Host $http_host;
}
cal uin = ngx.var.cookie_GINA_gray_SESSIONID
if uin ~= nil and uin == 'gray' then
ngx.say("Gray Environment World")
#ngx.exec("@gray_env")
else
ngx.exec("@product_env")
end