Hi!
agentzh, I thought about your proposal of processing the subrequest's body chunked via an iterator or a readable object, but it seems too complicated to implement (as in - it requires some major changes and would take more time).
I know subrequests aren't the best practice, but I could not find a better alternative.
Maybe you can help me? Here's what I want to do -
- I've got nginx defined as a reverse proxy to another site.
- I want to be able to read the requests coming in to the proxy, read them by chunks, use cosockets (to communicate with another process), and possibly alter the request. This is possible with ngx_lua.
- In some cases, I want to be able to read the responses coming from the proxied server in chunks, use cosockets (to communicate with another process), and possibly change the response. For that to work efficiently enough, I would sometimes like to decide whether I want to read the entire body or not based on the response header or even on the partial body of the response. If I won't handle it - I'd like to pass it along to the client. This is the problematic part, mainly because the header and body filters do not support cosockets.
My solution was to create a location for the proxy (for which the content handler is proxy_pass), and in the main location use lua content handler, that performs a subrequest, and that's why the change I described here fits my purpose (I know it doesn't really sound general, but maybe in the future it could be enhanced to be more general, i.e. having a lua callback called when maximum body size has reached).
I thought about the following alternative ideas:
1. Making cosockets available from lua header and body filters.
2 .Writing the subrequest and its processing in C (in which case, I'd have to integrate with the nginx reactor for communication operations).
Both ideas seem to be much more work (obviously, I'd like to write as much as I can in Lua rather than in C).
Do you have any alternatives?
Regards,
Aviram
On Sunday, March 17, 2013 11:58:50 PM UTC+2, agentzh wrote:
Hello!
On Sun, Mar 17, 2013 at 2:05 PM, Brian Akins wrote:
> This is one reason we've started moving away from using
> ngx.location.capture. To get the level of control you describe, it
> would greatly complicate the code, I think. We've started using Lua
> modules for almost everything rather than ngx.location.capture. We've
> been pleasantly surprised by the performance and the decreased memory
> footprint and enjoy much greater control of the data flow.
>
I must say this is a good move that I'd love to see :)
The Nginx subrequest approach is mostly for reusing the existing Nginx
C modules like ngx_proxy, which is (much) more complicated (and thus
potentially slower) than the pure Lua solution based on the cosocket
API.
But given that one of ngx_lua's goals is to script the existing (pure
C) Nginx ecosystem, I'm still willing to enhancing the existing
subrequest API in ngx_lua, if not adding too much complication to the
code base.
Best regards,
-agentzh