Skip to content

Instantly share code, notes, and snippets.

@fdv
Created September 9, 2014 05:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fdv/c7e673eda7c7b1777533 to your computer and use it in GitHub Desktop.
Save fdv/c7e673eda7c7b1777533 to your computer and use it in GitHub Desktop.
# In the main vhost config
proxy_cache_path /var/cache/nginx levels=1:2
keys_zone=microcache:5m max_size=1000m;
# In the location /
# Init cache bypass
set $no_cache "";
# If non GET/HEAD, don't cache & mark user as uncacheable for 1 second via cookie
if ($request_method !~ ^(GET|HEAD)$) {
set $no_cache "1";
}
# Drop no cache cookie if needed
if ($no_cache = "1") {
add_header Set-Cookie "<%= @frontend_cookie %>=1; Max-Age=2; Path=/";
add_header X-Microcachable "0";
}
# Bypass cache if no-cache cookie is set
if ($http_cookie ~* "<%= @frontend_cookie %>") {
set $no_cache "1";
}
# Bypass cache if flag is set
proxy_no_cache $no_cache;
proxy_cache_bypass $no_cache;
# Set cache zone
proxy_cache microcache;
# Set cache key to include identifying components
proxy_cache_key $scheme$host$request_method$request_uri;
# Cache 404 responses for 10 minutes
proxy_cache_valid 404 10m;
# Only cache valid HTTP 200 responses for 1 minute
proxy_cache_valid 200 1m;
# Serve from cache if currently refreshing
proxy_cache_use_stale updating;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment