The nginx cache is tremendously powerful and I believe that, when combined with openresty,
Note that I didn’t say do anything.
I spent a few hours a week over a year building a bunch of caches that that implemented very
complex business rules. Each page was a single Location block of 40 to 60 lines. I’ve attached one
at the end of this mail as an example.
The cache configuration options are very subtle, and the way that they interact isn’t linear or obvious
but it does make sense. The best way to debug is to use wget -S, with a custom .wgetrc that pretends to be a browser
Here’s an example of an nginx cache definition for a page that has quite a complex cache key:
# Blog pages get cached
location ~ /blog/ {
# Cleanup the cache-busting URLs used by the hackers who attacked in Nov 2012
rewrite "/(.*)/2];ord.*$" $1 ;
proxy_no_cache $arg_mid $arg_siteID;
proxy_cache_bypass $arg_mid $arg_siteID;
proxy_cache_use_stale updating;
default_type text/html;
proxy_cache_valid 200 302 301 15m;
proxy_ignore_headers Set-Cookie Cache-Control;
proxy_pass_header off;
proxy_hide_header Set-Cookie;
# this is to prevent our backend's Expires header from busting the cache.
# It doesn't actually enable browser caching because Akamai overwrites it
expires 900s;
add_header Last-Modified "";
add_header ETag "";
# Build cache key
set $e4x_currency $cookie_google_lang;
set_if_empty $google_lang ‘ENG';
set $num_items $cookie_NumberOfItems;
set_if_empty $num_items 'LOW';
set $nao $arg_Nao;
set_if_empty $nao '0_nao';
set $ns $arg_ns;
set_if_empty $ns '0_ns';
set $orig_uri $uri;
# If a page name contains a colon then cache it with a pipe sign instead
set_by_lua $key_uri '
local nu = string.gsub(ngx.var.orig_uri, ":", "|");
return nu;
';
set $brand_landing $arg_adminPage;
set_if_empty $admin_page 'false';
set $oper $cookie_OPER;
set_if_empty $oper '0_oper';
proxy_cache_key "$key_uri|$nao|$ns|$google_lang|$num_items|$oper|$admin_page";
proxy_cache blog_pages;
# Add Canonical URL string
set $folder_id $arg_FOLDER%3C%3Efolder_id;
add_header Link "<$canonical_url>; rel=\"canonical\"";
}