Nginx重定向代理响应。

我想知道是否可以使用nginx实现以下场景:

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

我猜nginx本身无法完成,但是可以使用lua脚本吗?

(如果你想知道我们要实现什么:客户端向我们的FE服务器(nginx)上传文件,它只是将文件发送到文件服务器(服务器“A”),然后我们需要获取文件服务器的响应并将其运行到另一个服务器“B”,该服务器构建适合用户的良好响应。

谢谢。

点赞
用户1226172
用户1226172

将下面翻译成中文并且保留原本的 markdown 格式

Its not possible possible with Nginx reverse proxy server.

You can call server-B from server-A to parse response using web-services .

无法使用 Nginx 反向代理服务器实现此功能。

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

2015-07-10 13:11:58
用户642214
用户642214

所以我使用 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;
}
2015-07-10 22:32:54