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

2.4k Views Asked by At

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.

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

There are 1 best solutions below

5
On BEST ANSWER

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