Skip to content

Instantly share code, notes, and snippets.

@math0ne
Created August 20, 2015 16:57
Show Gist options
  • Save math0ne/86335ce2133b5c3e1b53 to your computer and use it in GitHub Desktop.
Save math0ne/86335ce2133b5c3e1b53 to your computer and use it in GitHub Desktop.
# This is a basic VCL configuration file for varnish. See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition. Set this to point to your content
# server.
#
backend default {
.host = "216.14.119.170";
.port = "80";
}
# This is a basic VCL configuration file for varnish. See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition. Set this to point to your content
# server.
#
sub vcl_recv {
# mobile devices detection
call detect_device;
remove req.http.Etag;
if (req.url ~ "^/robots.txt$" && req.http.host == "mail.innerbody.com") {
error 702 "OK";
}
if (req.url ~ "^/robots.txt$" && req.http.host == "ads.ib-data.com") {
error 702 "OK";
}
if (req.url ~ "^/robots.txt$" && req.http.host == "ib-data.com") {
error 702 "OK";
}
if (req.url ~ "^/robots.txt$" && req.http.host == "216.14.119.196") {
error 702 "OK";
}
# Remove all cookies that Drupal doesn't need to know about. ANY remaining
# cookie will cause the request to pass-through to Apache. For the most part
# we always set the NO_CACHE cookie after any POST request, disabling the
# Varnish cache temporarily. The session cookie allows all authenticated users
# to pass through as long as they're logged in.
if (req.http.Cookie) {
set req.http.Cookie = ";" + req.http.Cookie;
set req.http.Cookie = regsuball(req.http.Cookie, "; +", ";");
set req.http.Cookie = regsuball(req.http.Cookie, ";(SESS[a-z0-9]+|NO_CACHE)=", "; \1=");
set req.http.Cookie = regsuball(req.http.Cookie, ";[^ ][^;]*", "");
set req.http.Cookie = regsuball(req.http.Cookie, "^[; ]+|[; ]+$", "");
if (req.http.Cookie == "") {
# If there are no remaining cookies, remove the cookie header. If there
# aren't any cookie headers, Varnish's default behavior will be to cache
# the page.
unset req.http.Cookie;
}
else {
# If there are any cookies left (a session or NO_CACHE cookie), do not
# cache the page. Pass it on to Apache directly.
return(pass);
}
}
}
sub detect_device {
if (req.http.User-Agent ~ "iP(hone|od)" ||
req.http.User-Agent ~ "Android") {
# Define smartphones and tablets
set req.http.X-Device = "smart";
set req.http.user-agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_4 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B350 Safari/8536.25";
} elseif (req.http.User-Agent ~ "iPad") {
set req.http.X-Device = "desktop";
set req.http.user-agent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201";
} elseif (req.http.User-Agent ~ "SymbianOS" ||
req.http.User-Agent ~ "^BlackBerry" ||
req.http.User-Agent ~ "^SonyEricsson" ||
req.http.User-Agent ~ "^Nokia" ||
req.http.User-Agent ~ "^SAMSUNG" ||
req.http.User-Agent ~ "^LG") {
# Define every other mobile device
set req.http.user-agent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201";
set req.http.X-Device = "other";
} else {
# Define the desktop device
set req.http.X-Device = "desktop";
set req.http.user-agent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201";
}
}
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
# add User-Agent to hash for mobile devices
hash_data(req.http.X-Device);
return (hash);
}
sub vcl_fetch {
unset beresp.http.Etag;
#set obj.ttl = 1m;
unset beresp.http.expires;
set beresp.ttl = 1w;
# If the request to the backend returns a code is 5xx, restart the loop
# If the number of restarts reaches the value of the parameter max_restarts,
# the request will be error'ed. max_restarts defaults to 4. This prevents
# an eternal loop in the event that, e.g., the object does not exist at all.
if (beresp.status >= 500 && beresp.status <= 599){
return(restart);
}
# set the vary header
#set beresp.http.Vary = beresp.http.Vary + ", User-Agent";
return(deliver);
}
sub vcl_deliver {
unset resp.http.Etag;
#if (resp.http.Vary) {
set resp.http.Vary = regsub(resp.http.Vary,
"(?i)User-Agent",
"");
#}
# set the vary header
set resp.http.Vary = resp.http.Vary + ", User-Agent";
set resp.http.X-Age = resp.http.Age;
unset resp.http.Age;
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
sub vcl_error {
if (obj.status == 702) {
set obj.status = 200;
set obj.http.Content-Type = "text/plain; charset=utf-8";
synthetic {"# robots.txt
User-agent: *
Disallow: /"};
}
return (deliver);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment