Help! log phase is not executed due to r->count being non-zero

Maxim Dounin mdounin at
Mon Oct 13 17:06:38 UTC 2014


On Mon, Oct 13, 2014 at 12:41:04PM -0400, wangweixun wrote:

> Maxim,
> You are right. Since I need to make a hash of the original request's body, I
> do make call to ngx_http_read_client_request_body().
> What's the best way to decrement the count then? Simply "r->count--;" before
> returning from the module.

The ngx_http_read_client_request_body() increments r->count as it 
expects that r->main->count will be decremented by twice:

- right after the ngx_http_read_client_request_body() call, by 
  ngx_http_finalize_request() as called automatically on you return 
  from content handlers;

- later in post_handler, by another ngx_http_finalize_request() 

If you do call ngx_http_read_client_request_body() from an access 
handler, it should be fine to do "r->main->count--" in 
post_handler directly.

(Note well that nginx generally doesn't assume that the request 
body will be read before the content phase, and there may be 
problems associated with this if you'll try reading request body 
in an access handler.)

> By the way, in my access phase module, I need to send an subrequest and wait
> for its response asynchornously. ngx_http_read_client_request_body() also
> works asynchornously with a callback function, which apparently is not able
> to send a subrequest. What I did is registering two handlers in access
> phase. The first one does nothing but reading the request body only. The
> second one does the real work (subrequest, etc.). Does it sound right?

No, it doesn't.  You should do the real work in post_handler.

Maxim Dounin

More information about the nginx mailing list