# Accept connections and close connection hooks in custom module

**URL:** <https://www.ruby-forum.com/t/accept-connections-and-close-connection-hooks-in-custom-module/221779>\
**Category:** NGINX\
**Created:** [July 30, 2012, 7:06pm UTC](https://www.ruby-forum.com/t/accept-connections-and-close-connection-hooks-in-custom-module/221779 "2012-07-30T19:06:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [July 30, 2012, 7:06pm UTC](https://www.ruby-forum.com/t/accept-connections-and-close-connection-hooks-in-custom-module/221779/1 "2012-07-30T19:06:45Z")

</div>

Hi all,

I’m currently porting Apache module to nginx and need to have  
preconnection and postconnection hook for custom code.  
In apache it is ap\_hook\_pre\_connection and apr\_pool\_cleanup\_register  
calls.

Is it possible in nginx?

Thanks in advance

Posted at Nginx Forum:

> **[Accept connections and close connection hooks in custom module](https://forum.nginx.org/read.php?2,229100,229100#msg-229100)**
>
> Hi all, I'm currently porting Apache module to nginx and need to have preconnection and postconnection hook for custom code. In apache it is ap\_hook\_pre\_connection and apr\_pool\_cleanup\_register calls. Is it possible in nginx? Thanks in advance

---

<div class="post-metadata">

**Author:** ![ConnorMcLaud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@ConnorMcLaud](https://www.ruby-forum.com/u/ConnorMcLaud)\
**Post date:** [August 20, 2012, 12:25pm UTC](https://www.ruby-forum.com/t/accept-connections-and-close-connection-hooks-in-custom-module/221779/2 "2012-08-20T12:25:28Z")

</div>

I think I’ve finally understood. There is global structure  
ngx\_event\_actions  
with add\_conn/del\_conn handlers which supposed to do what I want. There  
is  
only one issue (probably a bug) with add\_conn handler. In version 1.2.2  
with  
epoll usage handler add\_conn never called. You should use add handler  
instead and check incoming parameters (event = NGX\_READ\_EVENT, flags =  
NGX\_CLEAR\_REQUEST). Hope it helps someone.

Posted at Nginx Forum:

> **[Re: Accept connections and close connection hooks in custom module](https://forum.nginx.org/read.php?2,229100,229921#msg-229921)**
>
> Hi all, I'm currently porting Apache module to nginx and need to have preconnection and postconnection hook for custom code. In apache it is ap\_hook\_pre\_connection and apr\_pool\_cleanup\_register calls. Is it possible in nginx? Thanks in advance

---

<div class="post-metadata">

**Author:** ![ConnorMcLaud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@ConnorMcLaud](https://www.ruby-forum.com/u/ConnorMcLaud)\
**Post date:** [August 27, 2012, 2:00pm UTC](https://www.ruby-forum.com/t/accept-connections-and-close-connection-hooks-in-custom-module/221779/3 "2012-08-27T14:00:27Z")

</div>

Hello!

On Mon, Aug 20, 2012 at 06:25:09AM -0400, ConnorMcLaud wrote:

> I think I’ve finally understood. There is global structure ngx\_event\_actions  
> with add\_conn/del\_conn handlers which supposed to do what I want. There is  
> only one issue (probably a bug) with add\_conn handler. In version 1.2.2 with  
> epoll usage handler add\_conn never called. You should use add handler  
> instead and check incoming parameters (event = NGX\_READ\_EVENT, flags =  
> NGX\_CLEAR\_REQUEST). Hope it helps someone.

The add\_conn/del\_conn handlers isn’t what you want to touch from  
your module, it’s handlers for event modules to register  
connections in an event handling machinery. These hooks are  
called not only for client connections, but e.g. for nginx own  
internal connections as well, and for listening sockets also.

Moreover, nginx core is aware of aspects of various event methods  
supported and might not call add\_conn/del\_conn even if the are  
set. The issue with epoll you’ve hit is just one of the cases.

Right now there is no good way to hook new accepted connections  
unless you are writing your own core module which creates  
listening sockets by itself. Most recent point for correct hooks  
available as of now for http modules is NGX\_HTTP\_POST\_READ\_PHASE.

Maxim D.

---

<div class="post-metadata">

**Author:** ![ConnorMcLaud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@ConnorMcLaud](https://www.ruby-forum.com/u/ConnorMcLaud)\
**Post date:** [August 27, 2012, 2:01pm UTC](https://www.ruby-forum.com/t/accept-connections-and-close-connection-hooks-in-custom-module/221779/4 "2012-08-27T14:01:18Z")

</div>

In fact I mostly need catch client deconnection. There is no good way  
for  
that as for accepted connection, isn’t it?

Posted at Nginx Forum:

> **[Re: Accept connections and close connection hooks in custom module](https://forum.nginx.org/read.php?2,229100,229958#msg-229958)**
>
> Hi all, I'm currently porting Apache module to nginx and need to have preconnection and postconnection hook for custom code. In apache it is ap\_hook\_pre\_connection and apr\_pool\_cleanup\_register calls. Is it possible in nginx? Thanks in advance

---

<div class="post-metadata">

**Author:** ![ConnorMcLaud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c4cdca/32.png) [@ConnorMcLaud](https://www.ruby-forum.com/u/ConnorMcLaud)\
**Post date:** [September 1, 2012, 5:12pm UTC](https://www.ruby-forum.com/t/accept-connections-and-close-connection-hooks-in-custom-module/221779/5 "2012-09-01T17:12:15Z")

</div>

Hello!

On Tue, Aug 21, 2012 at 10:46:32AM -0400, ConnorMcLaud wrote:

> In fact I mostly need catch client deconnection. There is no good way for  
> that as for accepted connection, isn’t it?

Actually there is: add a cleanup handler for a connection pool.

Maxim D.
