tengine + lua 实现 固定 响应码请求转移

环境采用 tengine + lua 2.2.1<nginx 1.8.1>
nginx 安装及upstream + server的配置在这里不赘述了,网上一搜一堆
直接上demo 供大家参考,可以将502的请求转走,一般服务重启上线时会报502,可以用此方案将某些状态码转走

location /xianshangjiekou {
                root   html;
                index  index.html index.htm;
                proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
                proxy_next_upstream_timeout 1500;
                proxy_next_upstream_tries 2;
                proxy_pass http://test;
                include proxy.conf;
        }
        location /lua {

                content_by_lua '
                         ngx.req.read_body()                                                
                         local args = ngx.req.get_uri_args()
                         local request_uri = ngx.var.request_uri
                         local url = string.sub(request_uri,5,string.len(request_uri))
                         ngx.say(url)
      
                         res = ngx.location.capture(url)  
                         status = res.status
                         if(status == 404) then
                            res = ngx.location.capture("/other")
                            ngx.say("原地址是返回了404!")
                         end  
                                ngx.print(res.body)
                                ngx.say(res.status)
                         ngx.say("ok")                                                      
                '; 
        }
         location /other {
                content_by_lua_block {
                         ngx.say("dog = ", ngx.var.dog)
                          ngx.say("cat = ", ngx.var.cat)
                }
         }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值