Skip to content

Instantly share code, notes, and snippets.

@initcron
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save initcron/67ae5f872d7dc06926f6 to your computer and use it in GitHub Desktop.

Select an option

Save initcron/67ae5f872d7dc06926f6 to your computer and use it in GitHub Desktop.
varnish configurations
include "director.vcl";
include "devicedetect.vcl";
sub vcl_recv {
set req.backend = ikuna_director;
if (! req.backend.healthy) {
set req.grace = 5m;
} else {
set req.grace = 15s;
}
#Force Varnish to cache documents even when cookies are present
if (req.request == "GET" && req.http.cookie) {
return(lookup);
}
if (req.url ~ "\.(css|jpg|gif|jpeg|png|html)") {
remove req.http.cookie;
return (lookup);
}
if (req.url ~ "^/admin/.*$" ||
req.url ~ "^.*/admin/.*$" ||
req.url ~ "^/admin/.*$" ||
req.url ~ "^.*/admin/.*$" ||
req.url ~ "^.*/customer/.*$" ||
req.url ~ "^.*/account/.*$" ||
req.url ~ "^.*/login/.*$"
) {
return (pass);
}
#Mobile Devices Detect
# Detect Mobile Devices, Redirect them to 750, which calls vcl_error to redirect url
call devicedetect;
if (req.http.host ~ "^(www\.)?telemundoatlanta\.com$") {
# if (req.http.X-UA-Device ~ "^mobile" || req.http.X-UA-device ~ "^tablet") {
if (req.http.X-UA-Device ~ "^mobile" ) {
error 750 "Moved Temporarily";
}
}
##End of Mobile Devices Detect
if (req.url ~ "\.(css|jpg|gif|jpeg|png|html)") {
remove req.http.cookie;
return (lookup);
}
if (req.restarts == 0) {
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;
}
}
if (req.request != "GET" &&
req.request != "HEAD" &&
req.request != "PUT" &&
req.request != "POST" &&
req.request != "TRACE" &&
req.request != "OPTIONS" &&
req.request != "DELETE") {
/* Non-RFC2616 or CONNECT which is weird. */
return (pipe);
}
if (req.request != "GET" && req.request != "HEAD") {
/* We only deal with GET and HEAD by default */
return (pass);
}
if (req.http.Authorization || req.http.Cookie) {
/* Not cacheable by default */
return (pass);
}
if (client.ip == "80.28.213.70") {
return (pipe);
}
return (lookup);
}
sub vcl_pipe {
# Note that only the first request to the backend will have
# X-Forwarded-For set. If you use X-Forwarded-For and want to
# have it set for all requests, make sure to have:
# set bereq.http.connection = "close";
# here. It is not set by default as it might break some broken web
# applications, like IIS with NTLM authentication.
return (pipe);
}
sub vcl_pass {
return (pass);
}
sub vcl_hash {
hash_data(req.url);
if (req.http.host) {
hash_data(req.http.host);
} else {
hash_data(server.ip);
}
return (hash);
}
sub vcl_hit {
return (deliver);
}
sub vcl_miss {
return (fetch);
}
sub vcl_fetch {
set beresp.grace = 300s;
if (beresp.http.Set-Cookie) {
return(deliver);
}
if (req.url ~ "\.css$") {
set beresp.ttl = 120s;
}
if (req.url ~ "\.ajax$") {
set beresp.ttl = 20s;
}
if (req.url ~ "\.js$") {
set beresp.ttl = 120s;
}
if (req.url ~ "\.html$") {
set beresp.ttl = 180s;
}
if (req.url ~ "\.xml$") {
set beresp.ttl = 180s;
}
if (req.url ~ "\.htm$") {
set beresp.ttl = 180s;
}
# if (beresp.ttl < 10s) {
# /* If a ttl is set to 0 or less as per default TTL rules, so will obj.cacheable. You can overwrite
# * this, and you should if you are chaning from not caching to caching.
# */
# set beresp.ttl = 300s;
# }
if (beresp.ttl <= 0s ||
beresp.http.Set-Cookie ||
beresp.http.Vary == "*") {
/*
* Mark as "Hit-For-Pass" for the next 2 minutes
*/
set beresp.ttl = 120 s;
return (hit_for_pass);
}
if (req.url ~ "\.php$") {
set beresp.ttl = 0s;
}
if (req.url ~ "\.(swf|flv|mp4)") {
set beresp.ttl = 0s;
}
return (deliver);
}
sub vcl_deliver {
remove resp.http.X-Varnish;
remove resp.http.Via;
remove resp.http.Age;
remove resp.http.X-Powered-By;
remove resp.http.Server;
return (deliver);
}
sub vcl_error {
#Redirect URL for Mobile Devices
if (obj.status == 750) {
set obj.http.Location = "http://www.atlalamano.com" + req.url;
set obj.status = 302;
#return(deliver);
}
set obj.http.Content-Type = "text/html; charset=utf-8";
set obj.http.Retry-After = "5";
synthetic {"
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>"} + obj.status + " " + obj.response + {"</title>
</head>
<body>
<h1>Error "} + obj.status + " " + obj.response + {"</h1>
<p>"} + obj.response + {"</p>
<h3>Guru Meditation:</h3>
<p>XID: "} + req.xid + {"</p>
<hr>
<p>Varnish cache server</p>
</body>
</html>
"};
return (deliver);
}
sub vcl_init {
return (ok);
}
sub vcl_fini {
return (ok);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment