Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 721
YS_NE Avatar asked Mar 08 '23 01:03

YS_NE


1 Answers

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)
   }
}
like image 85
Tarun Lalwani Avatar answered Apr 06 '23 09:04

Tarun Lalwani