4

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.

YS_NE
  • 194
  • 2
  • 21

1 Answers1

7

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)
   }
}
Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
  • Thats perfect. I just need to replace single quotes with curly braces. But it works great. Its working fine when status is 200 for all. But need to see if http status code of one of apis is not 200, then that need to be reported separately. – YS_NE Sep 24 '17 at 14:30
  • @Yogi, yes i mixed `content_by_lua` and `content_by_lua_block` syntax by mistake, corrected now in answer. For call failure you just need to check `respA.status` and take action based on the status. – Tarun Lalwani Sep 24 '17 at 14:35
  • Great. So when finally ngx.say statement executes. Can I determine the http status code for this. Actually if at least one call fails out of all, I want to return NOT SUCCESS. Currently it is just returning 200 OK. How can I return 500. – YS_NE Sep 24 '17 at 19:04
  • Yes you can do `ngx.status=500`, but this must be done before any `ngx.say` happens else the code will be set to 200 – Tarun Lalwani Sep 24 '17 at 19:08
  • I noted this on https://stackoverflow.com/questions/55930850/is-it-possible-to-merge-two-json-responses-using-nginx as well but I think this is strictly worse than doing it at application level because you lose parallelism of requests, no? – Alex Moore-Niemi Nov 20 '20 at 20:51