[module] Tracking connection status from accepted to closed

Roman Arutyunyan arut at nginx.com
Mon Oct 30 11:49:04 UTC 2017


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().

> How should i implement my module? 
> Thanks, 
> _______________________________________________
> nginx-devel mailing list
> nginx-devel at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx-devel

Roman Arutyunyan

More information about the nginx-devel mailing list