Nginx cache grouped by keys


I work for a company which uses a lot the proxy module and the proxy
We always have problems on clearing the cache because it has to be done
by url.

Thinking in a way to solve this I made the patch in attachment
(nginx_cache_with_groups.patch (, for nginx
1.0.10 and 0001-adding-parameter-to-purge-cache-by-groups.patch (, for ngx_cache_purge 1.4).

Basically it changes the (fastcgi|proxy|scgi|uwsgi)_cache_key directives
adding an optional value named “group key”.
This value accept any variable available on nginx configuration file
parameters, headers,…) just like the key value.

When using this value a directory is created using the same structure of
levels to store all files which have the same group key.
Doing this we can purge the cache using a specific file, like is done
today, or all files from a group. (I change the cache_purge_module to
this possible too)

I will give an example to be clear.
Imagine the configuration attached (nginx.conf - and that “html/static” folder has some
like test.css, test1.html and test2.html.

With that configuration the extension file will be used as the group key
and the uri as cache key.

When we execute the following commands the corresponded files are
command: curl -v ‘http://localhost:8090/test1.html
-> where fc35fdc70d5fc69d269883a822c7a53e is from group key and
bcccd499fbe303221add1b6e57d693d1 from file key

command: curl -v ‘http://localhost:8090/test2.html
-> where fc35fdc70d5fc69d269883a822c7a53e is from group key and
558156b89af1ccd4844556ab699d6864 from file key

command: curl -v ‘http://localhost:8090/test.css
-> where c7a628cba22e28eb17b5f5c6ae2a266a is from group key and
9a96e0fe2f89d77e756fa45ae882df0a from file key

Now we can purge the cache from css file doing
curl -v ‘localhost:8090/purge/test.css’ -H ‘X-Group-Clean:0’

Or purge all html files doing
curl -v ‘localhost:8090/purge/_.html’ -H ‘X-Group-Clean:1’ (the
in _.html is only to do a match on location)

The Group key can be as general or as specific like we want/need.

The default value for directive group_clean_cache_purge is 0, and the
used to it must be a text which converted to a number (ngx_atoi) results
a zero, purge only the selected file, or non zero, purge all files from
selected group.

Another example of use is we have a service to resize images, and the
desired size is passed through the uri like
http://localhost/480x360/1361234.jpg or

With that patch we can group all resized images using the image id
(1361234, in the example) as the group key and than purge all cached
at once.

I hope this patch can be applied in future versions of Nginx and made
available to everyone.


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs