【问题标题】:Nginx reroute a proxied responseNginx 重新路由代理响应
【发布时间】:2015-07-11 06:32:54
【问题描述】:

不知能否用nginx实现如下场景:

  1. 接受一个发布请求(文件上传)并将其代理传递给某个后端服务器“A”。
  2. 从代理服务器“A”获取响应并将其发布到另一个后端服务器“B”。
  3. 最终得到服务器“B”的响应并发送给客户端。

我想我不能用 nginx ootb 做到这一点,但是 lua 脚本可以做到吗?

(如果您想知道我们试图实现什么:客户端将文件 POST 到我们的 FE 服务器(nginx),它只是将文件发送到文件服务器(服务器“A”),那么我们需要获取文件服务器响应并通过另一台服务器“B”运行它,该服务器为用户构建了一个很好的响应)。

谢谢。

【问题讨论】:

    标签: nginx lua


    【解决方案1】:

    所以使用 nginx lua 模块,这就是我想出的:

    location /upload {
        lua_need_request_body on;
        set $upres "";
        rewrite_by_lua '
            local res = ngx.location.capture("/doupload", {method = ngx.HTTP_POST, always_forward_body = true })
            ngx.var.upres = res.body
        ';
    
        content_by_lua '
            local res = ngx.location.capture("/afterupload", { method = ngx.HTTP_POST, body = ngx.var.upres })
            if res.status == 200 then
                ngx.print(res.body)
            end
        ';
    }
    
    location /doupload {
        proxy_pass http://ServerA;
    }
    
    location /afterupload {
        proxy_pass http://ServerB;
    }
    

    【讨论】:

      【解决方案2】:

      使用 Nginx 反向代理服务器是不可能的。

      您可以从服务器 A 调用服务器 B 以使用 Web 服务解析响应。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-26
        • 1970-01-01
        • 1970-01-01
        • 2017-02-21
        • 1970-01-01
        • 2011-11-25
        相关资源
        最近更新 更多