一个指定url转发的lua脚本

local json = require(“cjson”)

function getHttpParameter(paramName)
local request_method = ngx.var.request_method
if request_method == “GET” then
return getRequestParameter(paramName)
elseif request_method == “POST” then
return getPostParameter(paramName)
end;
end

function getRequestParameter(paramName)
_args = ngx.req.get_uri_args()
return _args[paramName]
end

function getPostParameter(paramName)
ngx.req.read_body() — 解析 body 参数之前一定要先读取 body
local arg = ngx.req.get_post_args()
for k,v in pairs(arg) do
–ngx.log(ngx.DEBUG, “[POST] key:”..k..” v:”..v)
if paramName == k then
return v
end
end
return “”
end

function checkVerifySessionForward()
local _authInfo = getHttpParameter(“authInfo”)
if _authInfo ~= nil then
_authInfo = string.gsub(_authInfo, ” “, “+”)
_authInfo = ngx.decode_base64(_authInfo)
ngx.log(ngx.INFO, _authInfo)
_authInfo = json.new().decode(_authInfo)
ngx.log(ngx.INFO, “===========================================================”.._authInfo[“channelId”])
ngx.log(ngx.INFO, “===========================================================”.._authInfo[“uid”])
if _authInfo[“channelId”] == “k2″ and _authInfo[“uid”] == “68e92f0a4a29858725__EXP_.” then
ngx.log(ngx.INFO, “===========================================================”..”set to forward”)
ngx.var.to_url = ngx.var.forward_url
return 1
end
end
return 0
end

function checkCreateOrderForward()
local channelId = getHttpParameter(“channelId”)
local uid = getHttpParameter(“uid”)
if channelId ~= nil and uid ~= nil then
if channelId == “wane” and uid == “wan20″ then
ngx.log(ngx.INFO, “===========================================================”..”set to forward”)
ngx.var.to_url = ngx.var.forward_url
return 1
end
end
return 0
end

function checkOrderDigitForward()
local tradeNo = getHttpParameter(“tradeNo”)
if tradeNo ~= nil then
local checkDigit = string.sub(tradeNo, 4, 4)
ngx.log(ngx.INFO, “===========================================================tradeNo/checkDigit”..tradeNo..”/”..checkDigit)
if checkDigit == ‘j’ then
ngx.log(ngx.INFO, “===========================================================”..”set to forward”)
ngx.var.to_url = ngx.var.forward_url
return 1
end
end
return 0
end

if ngx.re.match(ngx.var.request_uri, “/account/verify-session/”) then
return checkVerifySessionForward()

elseif ngx.re.match(ngx.var.request_uri, “/pay/create-order/”) then
return checkCreateOrderForward()

–开始按uid测试时pay-notify不转发,实际按渠道切换时再配置按渠道转发。
–elseif ngx.re.match(ngx.var.request_uri, “/pay-notify/musi163″) then
— ngx.var.to_url = ngx.var.forward_url
–elseif ngx.re.match(ngx.var.request_uri, “/pay-notify/yf”) then
— ngx.var.to_url = ngx.var.forward_url

elseif ngx.re.match(ngx.var.request_uri, “/pay/cancel-rder/”) then
return checkOrderDigitForward()

elseif ngx.re.match(ngx.var.request_uri, “/pay/verify-orer/”) then
return checkOrderDigitForward()

end

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>