[module] Tracking connection status from accepted to closed

Roman Arutyunyan arut at nginx.com
Mon Oct 30 11:56:21 UTC 2017

On Mon, Oct 30, 2017 at 02:49:04PM +0300, Roman Arutyunyan wrote:
> Hi,
> On Sat, Oct 28, 2017 at 05:23:48PM +0700, Hung Nguyen wrote:
> > Hello, 
> > 
> > I am writing a HTTP core module that track http connection to know when it’s accepted and closed within nginx.
> > 
> > What I am doing is register my module at PREACCESS_PHASE, and then adding a clean up callback to know when the connection closed (from nginx side). 
> > The problem is from my module i see nginx’s calling it twice, and calling my ngx_http_cleanup_t implement twice also. 
> If you have an internal redirect in your code (for example when the index module
> is used), then after the redirect the request will pass again through the
> majority of phases, including the preaccess phase.
> > My question is: If I want to write my module to do exactly 2 things: 
> > 
> > 1. Know when connection accepted by nginx 
> You can use the NGX_HTTP_POST_READ_PHASE phase instead of
> NGX_HTTP_PREACCESS_PHASE.  Consider the ngx_http_realip_module as an example.
> > 2. and know when connection closed by nginx 
> Use request pool cleanup - ngx_pool_cleanup_add().

The above of course works for requests, not connections.  However you can
track connections in a similar way by registering a cleanup handler and
searching it later from a phase handler.  Take a look at the above mentioned
realip module for details.

Roman Arutyunyan

More information about the nginx-devel mailing list