Nginx variable caching

I create a cache key with nginx based on a URI request and request parameters, which checks memcache directly and then serves the page from PHP-FPM if the cache key is not found. My problem is that many URLs have query string parameters that go in different orders and thus generate two or more separate cache keys for each response.

Cache Settings:

set $cache_key "$uri?$args"; 

Thus, the URLs that enter them with the query string parameters in different orders ultimately create several possible cache keys for the same type:

 http://example.com/api/2.2/events.json?id=53&type=wedding&sort=title&limit=10 http://example.com/api/2.2/events.json?id=53&limit=10&type=wedding&sort=title http://example.com/api/2.2/events.json?id=53&limit=10&sort=title&type=wedding 

Nauseum ad for n! capabilities...

The end result is that memcache often pops up much faster than needed, because I have potential n! -1 duplicate copies of cached content simply because query string parameters arrive in a different order. Is there a way that I can sort them alphabetically before setting the cache key to avoid this? Are there other ways to elegantly solve this problem?

+8
caching nginx
source share
2 answers

if you know which parameters are important for generating the cache, you can specify them manually. Based on your example, I wrote the following example:

 set $cache_key "$uri?id=$arg_id&type=$arg_type&sort=$arg_sort&limit=$arg_limit"; 

Or you can use the built-in perl and write your own function that will generate the cache key, see examples here http://wiki.nginx.org/Configuration#Embedded_Perl_examples

+2
source share

Presumably you are creating the links yourself, and not trying to reorder them in nginx, can you use the output repeater to make sure they are in sequential order when generating the pages?

0
source share

All Articles