Subrequest using Parent Request Body

Matthieu Tourne matthieu.tourne at
Wed May 12 23:15:22 MSD 2010

On Mon, May 10, 2010 at 6:41 PM, Maxim Dounin <mdounin at> wrote:

> Hello!
> On Fri, May 07, 2010 at 03:38:54PM -0700, Matthieu Tourne wrote:
> > I'm trying to send a POST Subrequest using the same request_body as the
> > Parent Request.
> > The subrequest is sent first, and once it has been finalized the parent
> > request goes through.
> > (this is inspired on Maxim's ngx_http_auth_request_module)
> >
> > It works almost fine, but if I try to make this subrequest to a location
> > containing a proxy_pass, I get some weird behavior. (The connection takes
> > forever to terminate).
> >
> > My guess is that the request_body gets read by the upstream, and
> discarded.
> > When the parent request goes through, that data is not available anymore.
> If you issue subrequest before request body was read by main
> request, you likely get SIGSEGV on an attempt to read it.  If you
> didn't - you probably just didn't used -DDEBUG_MALLOC while
> compiling nginx (or appropriate malloc options in your OS).  It's
> only possible to read request body in main request.
> > If I create a dummy request_body for the subrequest with temporary
> buffers,
> > it seems to work fine.
> >
> > I think I might be able to get around this by reading first the original
> > request_body using ngx_http_read_client_request_body(),
> > and then copying the content of r->request_body to temporary buffers in
> my
> > subrequest.
> There is no good/reliable way to preserve request body and use it
> for two upstream requests.  There are no problems with in-memory
> buffer (at least I'm not aware of), but once your request body is
> buffered to temporary file - it will be released by first
> successful upstream request (see ngx_http_upstream_send_response()
> in ngx_http_upstream.c).
> Using dupfd() on temporary file descriptor may be an option, but
> it's your responsibility to do it properly.
> Maxim Dounin

So the way I solved this is to make a full copy of r->request_body into
temporary bufs of the subrequest.

I would create a subrequest using ngx_http_subrequest(),
then use ngx_http_read_client_request_body(r, post_handler), to read
r->request_body and set a  function to copy it in the subrquest as the

I was wondering if this could be dangerous, if the subrequest could be fired
before r->request_body would be completely read, or is it done in a
sequential order ?

Concerning malloc, is :
in nginx.conf sufficient ?

Also, is it possible to something such as Valgrind on nginx workers ?

Thank you,


Matthieu Tourne
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list