local res1, res2, action action = ngx.var.request_method if action == "POST" then arry = {method = ngx.HTTP_POST, body = ngx.req.read_body()} else arry = {method = ngx.HTTP_GET} end if ngx.var.switch == "on" then res1, res2 = ngx.location.capture_multi { -- 分发请求的核心函数 { "/product" .. ngx.var.request_uri , arry}, { "/test" .. ngx.var.request_uri , arry}, } else res1, res2 = ngx.location.capture_multi { { "/product" .. ngx.var.request_uri , arry}, } end if res1.status == ngx.HTTP_OK then local header_list = {"Content-Length", "Content-Type", "Content-Encoding", "Accept-Ranges"} for _, i in ipairs(header_list) do if res1.header[i] then ngx.header[i] = res1.header[i] end end ngx.say(res1.body) else ngx.status = ngx.HTTP_NOT_FOUND end
我们来捋一捋这个流程: 背景:首先我们向外部注册的地址的是 /predict/fake,我们实际 web server 的地址是 /predict,然后一个生产的 upstream (product)一个测试用的 upstream(test),ok,这时候一个 /predict/fake 请求过来了发生了什么呢