Question

I am using nginx and redis in my website. For several items on my site, I want to add tracking params to their urls so that when a user clicks on an item, I can collect statistics of user usage apart from serving the content user requested. I am looking for methods on how to capture this statistics in redis from nginx, i.e. without hitting the background application.

For eg., suppose I have several rows of tables on my page and each table hold items in row/column format. So, for an item in {table: 2, row: 1, column: 3} if the actual url is: www.mysite.com/news/dodi, then I want to embed the url: www.mysite.com/news/dodi/day=29?table=2&row=1&column=3. When the user clicks on it, nginx will hit redis to fetch the content and also update the appropriate statistics (here can be increment day-29, table-2, ...).

Any suggestion on how I achieve this without hitting the background app?

Was it helpful?

Solution

For the tracking parameters, keep this type of work outside the scope of the users request. Don't make them pay the latency penalty for stats tracking. Cron a script that extracts the parameters from access.log and updates redis and/or database.

In terms of caching content, there is a recently released nginx redis module that works in much the same way as the memcached module. Only GET and SELECT are presently implemented. The assumption is that your application is populating the cache.

http://wiki.nginx.org/Nginx3rdPartyModules#HTTP_Redis_Module

Sample nginx.conf:

http
{
 ...
        server {
            location / {
                set $redis_db   "0";
                set $redis_key  "$uri?$args";
                redis_pass      127.0.0.1:6379;
                error_page      404 502 504 = @fallback;
             }

             location @fallback {
                 proxy_pass      backed;
             }
        }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top