Created
September 18, 2009 11:19
-
-
Save davidsmalley/189011 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Setup our director and configure the two backends | |
director doctype_director round-robin { | |
{ | |
.backend = { | |
.host = "172.20.0.70"; | |
.port = "http"; | |
.probe = { | |
.url = "/lbcheck/lbuptest"; | |
.timeout = 0.3 s; | |
.window = 8; | |
.threshold = 3; | |
} | |
} | |
} | |
{ | |
.backend = { | |
.host = "172.20.2.214"; | |
.port = "http"; | |
.probe = { | |
.url = "/lbcheck/lbuptest"; | |
.timeout = 0.3 s; | |
.window = 8; | |
.threshold = 3; | |
} | |
} | |
} | |
} | |
sub vcl_recv { | |
# Grace will cache if the backends are down - if all the backends are down then the | |
# cache will not clear for 1 hour thus hopefully keeping the read only part of the site up | |
if (req.backend.healthy) { | |
set req.grace = 30s; | |
} else { | |
set req.grace = 1h; | |
} | |
# Skip any requests to the load balancer test | |
if (req.url ~ "^/lbcheck/lbuptest$") { | |
return (pass); | |
} | |
# Normalize accept-encoding headers | |
# Some browsers will capitalise, include multiple Accept-Encoding options etc. | |
# In order to reduce the number of caches we keep with different encodings, | |
# normalize this header here. | |
if (req.http.Accept-Encoding) { | |
if (req.url ~ "\.(jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg)$") { | |
# No point in compressing these | |
remove req.http.Accept-Encoding; | |
} elsif (req.http.Accept-Encoding ~ "gzip") { | |
set req.http.Accept-Encoding = "gzip"; | |
} elsif (req.http.Accept-Encoding ~ "deflate") { | |
set req.http.Accept-Encoding = "deflate"; | |
} else { | |
# unkown algorithm | |
remove req.http.Accept-Encoding; | |
} | |
} | |
# Normalize the Host: header | |
# This ensures for caching purposes that all urls are seen as "doctype.com/uri" | |
# even if this is an asset request to assets1.doctype.com/uri | |
# ensure's we don't cache static assets multiple times | |
if (req.http.host ~ "^(assets\d\.)+doctype.com$") { | |
set req.http.host = "doctype.com"; | |
} | |
# This is where we decide on whether to cache or not | |
# Cache any images, javascript, css and remove any cookies | |
# Skip the cache if the user has the user_credentials cookie | |
# Skip the cache if the user is hitting a url where we'd like cookies | |
# Skip the cache if its not a GET or HEAD request | |
# Otherwise, bin the cookie and lookup in the cache | |
if (req.url ~ "\.(css|js|jpg|jpeg|gif|ico|png)\??\d*$") { | |
unset req.http.cookie; | |
lookup; | |
} else { | |
if (req.http.cookie ~ "user_credentials") { | |
pass; | |
} elsif (req.url ~ "^/login") { | |
pass; | |
} elsif (req.url ~ "^/activate") { | |
pass; | |
} elsif (req.url ~ "^/sessions") { | |
pass; | |
} elsif (req.url ~ "^/users/new") { | |
pass; | |
} elsif (req.url ~ "^/users") { | |
pass; | |
} elsif (req.url ~ "^/password_resets") { | |
pass; | |
} elsif (req.request != "GET" && req.request != "HEAD") { | |
pass; | |
} else { | |
unset req.http.cookie; | |
} | |
} | |
} | |
sub vcl_fetch { | |
# See vcl_recv | |
set obj.grace = 1h; | |
# cache CSS and JS files | |
if (req.url ~ "\.(css|js|jpg|jpeg|gif|ico|png)\??\d*$") { | |
unset obj.http.Set-Cookie; | |
} | |
if (req.http.cookie ~ "user_credentials") { | |
pass; | |
} elsif (req.url ~ "^/login") { | |
pass; | |
} elsif (req.url ~ "^/activate") { | |
pass; | |
} elsif (req.url ~ "^/sessions") { | |
pass; | |
} elsif (req.url ~ "^/users/new") { | |
pass; | |
} elsif (req.url ~ "^/users") { | |
pass; | |
} elsif (req.url ~ "^/password_resets") { | |
pass; | |
} elsif (req.request != "GET" && req.request != "HEAD") { | |
pass; | |
} else { | |
unset obj.http.Set-Cookie; | |
} | |
# If this is a 40x error, skip the cache | |
if (obj.status >=400 && obj.status <500) { | |
pass; | |
} | |
# If this is a 50x error, skip the cache | |
if (obj.status >=500 && obj.status <600) { | |
pass; | |
} | |
} | |
sub vcl_deliver { | |
# Neat little helper for checking you got caching right | |
# Adds an X-Cache: header to your http requests which says either | |
# HIT or MISS to indicate if the request was cached by varnish | |
if (obj.hits > 0) { | |
set resp.http.X-Cache = "HIT"; | |
} 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