微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ngx.location.capture 只支持相对路径,不能用绝对路径

ngx.location.capture 是非阻塞的,ngx.location.capture也可以用来完成http请求,但是它只能请求到相对于当前Nginx服务器的路径,不能使用之前的绝对路径进行访问,但是我们可以配合Nginx upstream实现我们想要的功能

Nginx.cong中的http部分添加如下upstream配置

upstream backend {
    server s.taobao.com;
    keepalive 100;
}
在example.conf配置如下location

     location ~ /proxy/(.*) {
        internal;
        proxy_pass http://backend/$1$is_args$args;
     }

lua 请求可以这么写:

local resp = ngx.location.capture("/proxy/search",{
    method = ngx.HTTP_GET,
    args = {q = "hello"}

})
if not resp then
    ngx.say("request error :",err)
    return
end
ngx.log(ngx.ERR,tostring(resp.status))

--获取状态码
ngx.status = resp.status

--获取响应头
for k,v in pairs(resp.header) do
    if k ~= "transfer-encoding" and k ~= "Connection" then
        ngx.header[k] = v
    end
end
--响应体
if resp.body then
    ngx.say(resp.body)
end

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐