Reading request body in PRE_ACCESS phase

Fasih faskiri.devel at
Fri Aug 29 07:53:56 UTC 2014

Thanks for the reply. I am also doing this. Basically, have a way to
see if it the body was read synchronously or asynchronously. If
synchronous, let nginx handle it else we have to do that ourselves
[The way your module does it]. But I thought there is a better way to
do this.

Btw, I think you have to set write_event_handler to empty. Basically,
if you dont set it, and there is a write_event (while the body is not
read), nginx would call core_run_phases which you werent expecting.

On Fri, Aug 29, 2014 at 2:16 AM, Yichun Zhang (agentzh)
<agentzh at> wrote:
> Hello!
> On Thu, Aug 28, 2014 at 10:34 AM, Fasih wrote:
>> I am trying to read the request body in pre_access phase. This seems
>> like a regular requirement but I dont seem to find a good way to do
>> this. Since the request body is read asynchronously, I have to do
>> phases++ and core_run_phases myself in the read_completion callback. I
>> also have to set the r->write_event_handler to empty because otherwise
>> a write event calls run_phases which I obviously dont want till I read
>> the body. I am trying to understand what is the correct way to do this
>> since I seem to be hacking my way around this.
> Incrementing r->phase_handler manually in your "post_read" handler is
> a bad idea. You should just call ngx_http_core_run_phases() there if
> it is not a recursive call (when you read the body in a single run).
> And in your pre-access handler, just return appropriate return values
> according to the state in your "ctx". If the request body is done, you
> set a flag in your "ctx" in your "post_read" handler, and your
> pre-access phase handler checks this flag and decides whether to
> return NGX_DECLINED to proceed to the next phase handler, or just
> return NGX_DONE to yield the control back to the nginx event loop.
> The ngx_form_input module reads the request body in the rewrite phase,
> which should be similar:
> Regards,
> -agentzh
> _______________________________________________
> nginx-devel mailing list
> nginx-devel at

More information about the nginx-devel mailing list