Let me describe what I am trying to do first.
I have a bunch of pictures in a directory called /images/*.(jpg|gif|png|blah blah|)
Now say these images are embedded in an html page and I dont really care which image or where its embedded.
For every 10th request for the same picture file (if possible) or for any picture I want to display a fixed image (e.g. trollface.jpg). thats it!
I have searched around a bit but i am not even sure what I am looking for. Rewrite might help but then its a permanent thing. this has got to do something with requests. I have heard perl scripts can be used with nginx. I can't write an nginx module (though I did bravely lookup the docs and then gave up)
Before you ask "But why don't you do it in application, noob?". This is a static files only server. The point is to not execute any binary at all.
I can think of one solution to your question without the need to hack nginx.
Shortly, it can be handled on the file-system level.
In more details, you can configure an
incron
script to be executed on theIN_ACCESS
event. This means that your script will be executed whenever a specific file is accessed (read). In this script, you can do whatever you want like counting the read accesses and them overwrite the file with another one (don't forget to keep a backup).Please, note that the server caching may affect this method. The file/image might not be read when it is cached by the server or an intermediate proxy server.
I am assuming Linux platform. You may need to install the
incron
pacakge.Randomly.. I fall upon this.
http://agentzh.org/misc/slides/nginx-conf-scripting/nginx-conf-scripting.html#1
THis seems much easier than writing an entire NGINX module. Basically we use the already made nginx modules except it seems I have to use memcache to save counters. But at least it looks possible.
There are updated examples for memcache and subrequests. It will still slow down nginx a bit due to "if" calls but not too much. I would prefer Khaled's method if I knew how to go about it.
Another solution could be to have nginx proxy to itself. Set up two upstream vhosts, one which serves your normal images and one which serves trollface.jpg only.
Include the normal vhost as your upstream server with a weight of 9 and the trollface vhost as the other upstream server with a weight of 1.
This will serve the alternate image once in every 10 requests but not once in every 10 requests for each individual file.
Alternatively, the HttpEchoModule might be able to do the trick for you. A combination of echo_subrequest_async and echo_random could give you a 1 in 10 chance of delivering trollface.jpg instead of the file they requested.
There is also this post on the mailing list which suggests that someone is working on per-IP-address counters in the nginx config.
Note that I am not recommending any of these solutions, just noting that they're possible.