环境采用 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)
}
}