Hello!
On Sun, Oct 6, 2013 at 9:08 AM, Jędrzej Nowak wrote:
> Wouldn't it cause of reading whole body into memory, and then passing it
> from memory ?
>
Both "lua_need_request_body on" and ngx.req.read_body() use the
built-in request body reader in the Nginx core, which always buffers
the whole requesty body in either the memory or a temporary file
(depending on whether your request body size is bigger than the
client_body_buffer_size setting in nginx.conf). As long as you use
Nginx core's builtin request body reader, you have to buffer the whole
body, no matter it is the ngx_lua module or nginx standard modules
like ngx_proxy, ngx_fastcgi, or ngx_dav.
The ngx_lua module provides, however, an alternative request body
reader itself, that supports streaming processing:
http://wiki.nginx.org/HttpLuaModule#ngx.req.socket
With this ngx.req.socket API, you can read the request body data in
chunks and pass them to the backend services via the cosocket API at
the same time. Note that, for streaming request body processing, you
cannot use subrequests and traditional nginx modules as the subrequest
targets, because traditional nginx modules like ngx_proxy and
ngx_fastcgi always expect the whole requesty body to be ready (because
they assume that Nginx core's builtin requesty body reader is used).
One real-world example using this ngx.req.socket API to read big
request bodies in chunks is the lua-resty-upload library that reads
and parses multi-part request body data in a streaming fashion:
https://github.com/agentzh/lua-resty-upload
> I thought that:
>
> always_forward_body
>
[...]
>
> Will forward it without copying...
>
When this option is set and in effect, the request body read by the
builtin Nginx request body reader will be directly forwarded to the
subrequest without copying the whole request body data when creating
the subrequest (matter the request body data is buffered in memory
buffers or temporary files). Because you're using subrequests, you
have to buffer the whole request body anyway.
Regards,
-agentzh