Notes on caching static assets with nginx
I used to keep an elaborate map of cache rules per file type. Most of it was cargo cult. The version I have settled on is two rules.
location /assets/ {
expires 30d;
add_header Cache-Control "public, immutable";
}
location / {
expires 10m;
}
HTML is cheap to revalidate and I want edits to show up quickly, so it gets ten minutes. Assets change rarely and get a month. When I do change the stylesheet I rename it, which sidesteps the whole invalidation question.
Compression is the other half. Turning on gzip for text types cut transfer size by roughly two thirds here, for one line of configuration.