Hi folks,
I've read here about how one can use ngx.location.capture to implement a reverse proxy, but that it might be more efficient to use lua-resty-http to handle processing a proxied request. I'm curious if there is some way to use either of these to do the following:
(a) make a backend request
(b) send the response right back to the user as it is being read from (a)
(c) post-process or process via coroutine the same response (a) w/ access to cosockets
For example, let's say I wanted to build a transparent reverse proxy that saved a copy of the entire response to a remote service for a test engine. One way to do this, obviously is to use either ngx.location_capture or lua-resty-http to proxy the request to the backend, gather up the entire response, send the response to the remote testing service, and then finally ngx.print the response back to the client that made the originating request.
That technique works fine for smaller responses, but if a response is huge, it introduces the problem of letting the originating client see a long delay before their request starts receiving a response. E.g., if it takes 10 seconds for nginx to fully fetch the response from the backend, the originating client sees 10-plus seconds elapse before they start getting their response.
If I wanted to trade memory for response speed, I'd love a way to either process a copy of the response in a coroutine, or post-process the response after it was fully sent to the originating client. I'd have tried to reach for header_filter_by_lua_block and body_filter_by_lua_block but the cosocket libraries aren't available there so there isn't any way for me to send the copy of the response off to the remote testing service.
I am under the impression ngx_srcache implements handling of the response after the response has been sent, but I was hoping there was some way to do this via the LUA code. Is there a technique for managing this short of implementing a custom proxy client using the openresty cosocket library?
Jim