Nginx+ lua实现http转发请求

最近要使用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
	'; 
}

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值