最近要使用nginx+lua实现 一个需求:
在nginx的location部分,请求时,判断用户是否为会员,如果是会员,则跳转到a页面,否则跳转到b页面。
用户服务是一个单独的服务,具体lua脚本实现代码如下:
upstream tuc_sgin {
server 127.0.0.1:9806;
server 127.0.0.1:9807;
}
location /tuc_sgin_proxy/ {
add_header Access-Control-Allow-Origin *;
proxy_pass http://tuc_sgin/;
}
#如果vip_status=1跳转到id=35的页面,否则跳转到id=34的页面
location /sgin_valuable/ {
add_header Access-Control-Allow-Origin *;
proxy_pass https://127.0.0.1:9808/test/news/spread.html?id=35;
rewrite_by_lua '
local cjson = require("cjson");
local request_method = ngx.var.request_method
local args = nil
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST" == request_method then
ngx.req.read_body()
args = ngx.req.get_post_args()
end
local id = args["id"];
if id == nil or id == "" then
return ngx.redirect("https://127.0.0.1:9809/test/news/spread.html?id=34");
end
local res = ngx.location.capture("/tuc_sgin_proxy/tuc/social?
serviceType=query_userextSingle", {
method = ngx.HTTP_GET, args = args})
local result = cjson.decode(res.body);
if result["error_no"] == "0" then
local vip_status = result["data"]["vip_status"];
if vip_status ~= "1" then
return ngx.redirect("https://127.0.0.1:9809/test/news/spread.html?id=34");
end
elseif result["error_no"] ~= "0" then
return ngx.redirect("https://127.0.0.1:9809/test/news/spread.html?id=34");
end
';
}