是否可以在NGINX中合并多个响应并发送一个响应 [英] Is it possible to consolidate multiple responses and send one response in NGINX

查看:295
本文介绍了是否可以在NGINX中合并多个响应并发送一个响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个VM上运行了Nginx/openresty和其他一些服务.基本上,VM接受Openresty上的请求,然后openresty将请求转发到适当的服务.例如下面的请求分别转发给ServiceA,ServiceB和ServiceC.一切正常.

I have Nginx/openresty and some other services running on one VM. Basically VM accepts requests on Openresty and then openresty forwards requests to appropriate service. e.g. below requests getting forwarded to ServiceA, ServiceB and ServiceC respectively. It is working fine.

  • http://server:80/services/refA
  • http://server:80/services/refB
  • http://server:80/services/refC

现在,我需要公开一个新端点,该端点可以从所有服务A,B和C获得响应,然后返回一个合并响应.

Now I need to expose a new endpoint which could get the responses from all services A, B and C. and then return one consolidated response.

我无法在自己的位置使用多个proxy_pass,有人可以建议我如何实现该目标吗?例如

I cannot use multiple proxy_pass in my location, could someone suggest how can I achieve that? e.g.

http://server:80/services/refALL ->从以下位置返回合并的响应A,B和C服务.

http://server:80/services/refALL --> returns a consolidated response from A, B and C Services.

推荐答案

您可以按照以下步骤进行操作.基本上,您可以捕获其他服务的响应,然后将它们组合起来

you can do it like below. Basically you capture response from other services and then combine them

location /services/refALL {
   content_by_lua_block {
      local respA = ngx.location.capture("/services/refA")
      local respB = ngx.location.capture("/services/refB")
      local respC = ngx.location.capture("/services/refC")

      ngx.say(respA.body .. respB.body .. respC.body)
   }
}

这篇关于是否可以在NGINX中合并多个响应并发送一个响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆