Commit 312a5374 authored by daidekun's avatar daidekun

lua

parent 67dbeea3
server {
listen 80;
server_name _;
location / {
root /home/quant_group/project/public;
try_files $uri $uri/ /index.html;
}
}
\ No newline at end of file
local args = ngx.req.get_uri_args()
local register_uri = args['registerFrom']
local merchant_uri = args['merchantId']
if (register_uri) then
ngx.header['Set-Cookie'] = {'registerFrom=' .. register_uri .. '; path=/;'}
end
if (merchant_uri) then
ngx.header['Set-Cookie'] = {'merchantId=' .. merchant_uri .. '; path=/;'}
end
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
access_log logs/access.log combined;
server {
listen 8100;
server_name sc.xyqb.com;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml application/javascript;
gzip_vary on;
# 正常发布路径 和 灰度发布路径
set $rootPath "/Users/xuezhijie/playground/nginx/test1/public";
set $gatedRootPath "/Users/xuezhijie/playground/nginx/test1-gated/public";
# jenkins灰度渠道参数
set $gatedStr "registerFrom=1,2&merchantId=2,3";
# 解析灰度配置
# 1,2
set_by_lua_file $regConfig confs/parseConfig.lua $gatedStr "registerFrom";
# 2,3
set_by_lua_file $merchantConfig confs/parseConfig.lua $gatedStr "merchantId";
location / {
echo "http_cookie: $http_cookie";
# 获取cookie渠道号,判断是否属于灰度范围
echo "registerFrom渠道号: $cookie_registerFrom";
echo "merchantId渠道号: $cookie_merchantId";
echo "灰度配置registerFrom: $regConfig";
echo "灰度配置merchantId: $merchantConfig";
set_by_lua_file $isGray confs/isGray.lua $regConfig $merchantConfig $cookie_registerFrom $cookie_merchantId;
echo $isGray;
# 按渠道发布
if ($isGray != '') {
set $rootPath $gatedRootPath;
}
root $rootPath;
try_files $uri $uri/ /index.html;
}
}
}
\ No newline at end of file
--[[ 参数:
registerFrom 灰度设置
merchantId 灰度设置
cookie registerFrom值
cookie merchantId值
]]
local regConfig = ngx.arg[1];
local merConfig = ngx.arg[2];
local regCookie = ngx.arg[3];
local merCookie = ngx.arg[4];
local isGrayReg = false;
local isGrayMer = false;
if (regConfig ~= '' and regCookie~= '' and string.find(regConfig, regCookie)) then
isGrayReg = true;
end
if (merConfig ~= '' and merCookie ~= '' and string.find(merConfig, merCookie)) then
isGrayMer = true;
end
local isGray = '';
if (isGrayReg or isGrayMer) then
isGray = '1';
end
--print(isGray)
return isGray;
\ No newline at end of file
local t = "";
local str = ngx.arg[1];
local prop = ngx.arg[2];
-- local str = "registerFrom=1,2,3&merchantId=2,3";
-- local prop = "merchantId";
for k,v in string.gmatch(str, "(%w+)=([%w,]+)") do
if (k == prop) then
-- print(k, v)
t = v;
break;
end
end
return t;
\ No newline at end of file
function string.split(str)
local splitlist = {};
string.gsub(str, '[^,]+', function(w) table.insert(splitlist, w) end )
return splitlist;
end
function isInTable(value, tbl)
for k,v in ipairs(tbl) do
if v == value then
return true;
end
end
return false;
end
local path = ngx.var.normalPath;
local channelPath = ngx.var.channelPath;
-- 没有设置渠道,返回旧目录
if (ngx.var.registerChannel == '' and ngx.var.merchantChannel == '') then
return path;
end
-- 解析渠道设置
local register = string.split(ngx.var.registerChannel);
local merchant = string.split(ngx.var.merchantChannel);
-- cookie值
local register_cookie = ngx.var.cookie_registerFrom
local merchant_cookie = ngx.var.cookie_merchantId
-- uri参数值
local args = ngx.req.get_uri_args()
local register_uri = args['registerFrom']
local merchant_uri = args['merchantId']
-- 优先匹配参数中的registerFrom
if (register_uri) then
hitRegister = isInTable(register_uri, register)
else
hitRegister = isInTable(register_cookie, register)
end
-- 优先匹配参数中的merchantId
if (merchant_uri) then
hitMerchantId = isInTable(merchant_uri, register)
else
hitMerchantId = isInTable(merchant_cookie, register)
end
local baiTiao = '222';
--
if (register_uri) then
isBaitiao = register_uri == baiTiao
else
isBatitiao = register_cookie == baiTiao
end
-- 是白条,未设置merchantId规则,命中
if (isBaitiao and ngx.var.merchantChannel == '') then
return channelPath
end
-- 是白条,设置了merchantId规则,匹配merchantId后命中
if (isBaitiao and ngx.var.merchantChannel ~= '' and hitMerchantId) then
return channelPath
end
-- 不是白条,匹配registerFrom后命中
if ( not isBaitiao and hitRegister) then
return channelPath
end
return path;
function string.split(str)
local splitlist = {};
string.gsub(str, '[^,]+', function(w) table.insert(splitlist, w) end )
return splitlist;
end
function isInTable(value, tbl)
for k,v in ipairs(tbl) do
if v == value then
return true;
end
end
return false;
end
local path = ngx.var.normalPath;
local channelPath = ngx.var.channelPath;
if (ngx.var.registerChannel == '' and ngx.var.merchantChannel == '') then
return path;
end
local register = string.split(ngx.var.registerChannel);
local merchant = string.split(ngx.var.merchantChannel);
local register_cookie = ngx.var.cookie_registerFrom
local merchant_cookie = ngx.var.cookie_merchantId
local args = ngx.req.get_uri_args()
local register_uri = args['registerFrom']
local merchant_uri = args['merchantId']
if (register_uri) then
hitRegister = isInTable(register_uri, register)
else
hitRegister = isInTable(register_cookie, register)
end
if (merchant_uri) then
hitMerchantId = isInTable(merchant_uri, register)
else
hitMerchantId = isInTable(merchant_cookie, register)
end
local baiTiao = '222';
local isBaitiao = (register_cookie == baiTiao or register_uri == baiTiao)
if (isBaitiao and ngx.var.merchantChannel == '') then
return channelPath
end
if (isBaitiao and ngx.var.merchantChannel ~= '' and hitMerchantId) then
return channelPath
end
if ( not isBaitiao and hitRegister) then
return channelPath
end
return path;
server {
listen 7007;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 4;
gzip_types text/plain application/x-javascript text/css application/xml application/javascript;
gzip_vary on;
set $normalPath "/home/quant_group/paycenter-ui/dist";
set $channelPath "/home/quant_group/new-paycenter-ui/dist";
set $registerChannel "758,222,198";
set $merchantChannel "";
location / {
expires -1;
set_by_lua_file $rootPath /usr/local/openresty/nginx/conf/conf.d/lua/toChannel.lua;
header_filter_by_lua_file /usr/local/openresty/nginx/conf/conf.d/lua/addCookie.lua;
root $rootPath;
try_files $uri $uri/ /index.html?$query_string;
}
}
server {
listen 7001;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 4;
gzip_types text/plain application/x-javascript text/css application/xml application/javascript;
gzip_vary on;
set $normalPath "/home/quant_group/xyqb-ui/dist/public";
set $channelPath "/home/quant_group/xjd-ui/dist";
set $registerChannel "864,158744";
set $merchantChannel "";
location / {
expires -1;
set_by_lua_file $rootPath /usr/local/openresty/nginx/conf/conf.d/lua/toChannel.lua;
header_filter_by_lua_file /usr/local/openresty/nginx/conf/conf.d/lua/addCookie.lua;
root $rootPath;
try_files $uri $uri/ /index.html?$query_string;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment