Welcome! Log In Create A New Profile

Advanced

Nginx cache grouped by keys

November 29, 2011 03:34PM
Hi,

I work for a company which uses a lot the proxy module and the proxy cache
feature.
We always have problems on clearing the cache because it has to be done url
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 (query
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 make
this possible too)

I will give an example to be clear.
Imagine the configuration attached (nginx.conf) and that "html/static"
folder has some files 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 created:
command: curl -v 'http://localhost:8090/test1.html'
file:
../e/53/a/fc35fdc70d5fc69d269883a822c7a53e/1/3d/9/bcccd499fbe303221add1b6e57d693d1
-> where fc35fdc70d5fc69d269883a822c7a53e is from group key and
bcccd499fbe303221add1b6e57d693d1 from file key

command: curl -v 'http://localhost:8090/test2.html'
file:
../e/53/a/fc35fdc70d5fc69d269883a822c7a53e/4/86/6/558156b89af1ccd4844556ab699d6864
-> where fc35fdc70d5fc69d269883a822c7a53e is from group key and
558156b89af1ccd4844556ab699d6864 from file key

command: curl -v 'http://localhost:8090/test.css'
file:
../a/66/2/c7a628cba22e28eb17b5f5c6ae2a266a/a/f0/d/9a96e0fe2f89d77e756fa45ae882df0a
-> 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 underline
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 value
used to it must be a text which converted to a number (ngx_atoi) results in
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 http://localhost/x240/1361234.jpg

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 files
at once.

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

Regards,
Wandenberg
_______________________________________________
nginx-devel mailing list
nginx-devel@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx-devel
Subject Author Views Posted

Nginx cache grouped by keys Attachments

wandenberg 2128 November 29, 2011 03:34PM

Re: Nginx cache grouped by keys

Piotr Sikora 1278 December 05, 2011 10:18AM

Re: Nginx cache grouped by keys

Brian Akins 702 December 05, 2011 04:22PM

Re: Nginx cache grouped by keys

Piotr Sikora 1045 December 05, 2011 04:38PM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 107
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready