Is it possible to consolidate multiple responses and send one response in NGINX

久未见 提交于 2019-12-11 17:24:57

问题


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

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.

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

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


回答1:


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)
   }
}


来源:https://stackoverflow.com/questions/46267497/is-it-possible-to-consolidate-multiple-responses-and-send-one-response-in-nginx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!