Skip to content

Instantly share code, notes, and snippets.

@onyxfish
Created June 28, 2011 21:34
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save onyxfish/1052300 to your computer and use it in GitHub Desktop.
Save onyxfish/1052300 to your computer and use it in GitHub Desktop.
ChicagoNow Varnish configuration (development version)
backend app1 {
.host = "127.0.0.1";
.port = "8000";
}
acl purge {
"127.0.0.1";
"::1";
}
sub vcl_recv {
# Filter PURGE requests
if (req.request == "PURGE") {
if(!client.ip ~ purge) {
error 405 "Not allowed.";
}
# Purge URLs matching a regex
purge("req.url ~ " req.url " && req.http.host == " req.http.host);
error 200 "Purged";
}
# Append X-Forwarded-For header
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For = req.http.X-Forwarded-For ", " client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
# Only localhost is allowed to hit PHP files directly, and they are never cached
# This includes access to pingers
if (req.url ~ "^(/fabfile.py)|(/scripts/.*)|(/wp-content/.*\.php)$") {
if(client.ip ~ purge) {
return(pass);
}
error 405 "Not allowed.";
}
# Pass through search requests (not worth the memory to cache them)
if (req.url ~ "^/search/") {
return(pass);
}
# Pass through admin requests (never cache)
if (req.url ~ "(wp-admin|wp-comments-post|preview=true|tcc.async-upload)") {
return(pass);
}
# Don't cache Wordpress core files (avoid crazy side-effects; wp-cron)
if (req.url ~ "^/wp-.*\.php") {
return(pass);
}
# Strip cookies from static media
if (req.url ~ "^/.+\.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|swf|bmp|pdf|doc|docx|xls|xlsx|csv|avi|mov|mp3|flv|mp4)(\?.*)?$") {
unset req.http.cookie;
}
# Strip cookies
unset req.http.cookie;
# Enable a grace period: http://www.varnish-cache.org/wiki/VCL#Grace
set req.grace = 2m;
}
sub vcl_fetch {
# Don't cache things which specifically say not to
if (beresp.http.Cache-Control ~ "no-cache") {
return(pass);
}
# Pass through admin requests (never cache)
if (req.url ~ "(wp-admin|wp-comments-post|preview=true|tcc.async-upload)") {
return(deliver);
}
# Don't cache Wordpress core files (avoid crazy side-effects; wp-cron)
if (req.url ~ "^/wp-.*\.php") {
return(pass);
}
# Apply caching headers -- this caching strategy from http://varnish-cache.org/wiki/VCLExampleLongerCaching
if (beresp.cacheable) {
# Static media
if (req.url ~ "^/.+\.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|swf|bmp|pdf|doc|docx|xls|xlsx|csv|avi|mov|mp3|flv|mp4)(\?.*)?$") {
set beresp.ttl = 1w;
set beresp.http.cache-control = "max-age = 604800, public";
# HTML, etc.
} else {
set beresp.ttl = 5m;
set beresp.http.cache-control = "max-age = 300, must-revalidate";
}
set beresp.http.cached_headers_marker = "1";
}
# Process Edge-side includes
# Not currently being used on ChicagoNow
#esi;
# Enable a grace period: http://www.varnish-cache.org/wiki/VCL#Grace
set req.grace = 2m;
}
sub vcl_deliver {
# Reset Age header for reasons somewhat mysterious, see URL above re: caching strategy
if (resp.http.cached_headers_marker) {
unset resp.http.cached_headers_marker;
set resp.http.age = "0";
}
# Add caching debugging headers
if (obj.hits > 0) {
set resp.http.X-Cache = "Hit";
set resp.http.X-Cache-Hits = obj.hits;
} else {
set resp.http.X-Cache = "Miss";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment