# Counters in nginx

**URL:** <https://www.ruby-forum.com/t/counters-in-nginx/202988>\
**Category:** NGINX\
**Created:** [February 10, 2011, 3:08am UTC](https://www.ruby-forum.com/t/counters-in-nginx/202988 "2011-02-10T03:08:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [February 10, 2011, 3:08am UTC](https://www.ruby-forum.com/t/counters-in-nginx/202988/1 "2011-02-10T03:08:11Z")

</div>

Hi,

I need to realize event counters in nginx.  
So I’ve planned to create command like “counter”.  
And have something similar to HttpLimit(Zone/Req)Module,  
so my config should looks like:  
http {  
counter\_zone one $binary\_remote\_addr 10m;  
…  
server {  
…  
location / {  
count one $my\_event; #( $my\_event can be 0 or 1 )  
}  
}  
}  
and have extra location like  
location /my\_one\_stats { # will give stats to external script for  
specific argument;  
counter\_handler one $args\_one;  
}

location /my\_one\_stats\_reset { # will reset stats for specific argument;  
counter\_reset one $args\_one;  
}

In this case to be insured that my counter working in final phase I have  
to register this handler in NGX\_HTTP\_LOG\_PHASE phase, otherwise I  
wouldn’t know for sure that event is done.  
But if I’ll register it on init like:  
h = ngx\_array\_push(&cmcf-\>phases[NGX\_HTTP\_LOG\_PHASE].handlers);  
my handler will be called after HttpLogModule, so if I’ll try to log  
counter only previous value will be logged.

So my question:

1. May be exists more effective way to realize it, or it already  
realized?
2. What the best way to register handler before HttpLogModule but in  
NGX\_HTTP\_LOG\_PHASE?

Regards
