Hit counter persistent variables

Igor Sysoev is at rambler-co.ru
Wed Aug 20 18:49:57 MSD 2008

On Wed, Aug 20, 2008 at 09:40:42PM +0700, Chris Savery wrote:

> Thanks for the suggestion. I don't really want to go to that extent and 
> I'd wonder if updating a database would just slow the static files down. 
> What I had in mind is just incrementing a variable. I suppose I could 
> write a simple module that maintains a few in memory varibles. Just 
> wondered if there was an easier way.
> Maybe write a log format that just adds a single char for each hit. Then 
> later I can find hit count by checking the length of that log file? And 
> a cron once an hour that stores the file length in another file and 
> deletes the log so it repeats. But is it possible to just write one char 
> to a log file? Maybe log format like,
> log_format hits 'x';
> Just an idea. Would that be very fast?

The simple log is better than db because it has lesser overhead.
The single byte is not possible because nginx adds new line, but
you just need to divide the size by two. The access_log should be

      access_log  path/to/log   hits  buffer=128k;

> Manlio Perillo wrote:
> >Chris Savery ha scritto:
> >>Hello,
> >>
> >>Is there a way to have hit counter variables that would save every 
> >>hour or persist across restarts?
> >>
> >>I'm working on a photography site that I expect to be serving a huge 
> >>number of thumbnail (static) images. I don't need full logging of the 
> >>thumbnails so I've turned if off for that location but it sure would 
> >>be nice to keep some tracking of how many are served and perhaps at 
> >>what rate. I thought maybe just a counter variable that increments on 
> >>each hit in that location but it wouldn't be too useful unless it 
> >>could save to file from time to time, or perhaps append a count to a 
> >>file every hour so there is a time series log.
> >>
> >
> >
> >You can write an Nginx module that use, as an example, gdbm to store 
> >data, protected with a lock.
> >
> >
> >
> >Manlio Perillo
> >
> >

Igor Sysoev

More information about the nginx mailing list