Skip to content

Instantly share code, notes, and snippets.

@desarrolla2
Created October 29, 2013 15:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save desarrolla2/7216729 to your computer and use it in GitHub Desktop.
Save desarrolla2/7216729 to your computer and use it in GitHub Desktop.
default.vcl
backend default {
.host = "127.0.0.1";
.port = "8080";
}
# vcl_recv (yes, we’re skimpy with characters, it’s Unix) is called at the beginning of a request, after the complete request has been received and parsed. Its purpose is to decide whether or not to serve the request, how to do it, and, if applicable, which backend to use.
# In vcl_recv you can also alter the request. Typically you can alter the cookies and add and remove request headers.
# Note that in vcl_recv only the request object, req is available.
sub vcl_recv {
# Serve objects up to 2 minutes
set req.grace = 2m;
set req.http.X-Forwarded-For = client.ip;
set req.http.Host = regsub(req.http.Host, ":[0-9]+", "");
set req.backend = default;
if (req.request == "PURGE") {
error 405 "Not allowed.";
return(lookup);
}
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);
}
# Remove cookies set by Google Analytics (pattern: '__utmABC')
if (req.http.Cookie) {
set req.http.Cookie = regsuball(req.http.Cookie, "(^|; ) *__utm.=[^;]+;? *", "\1");
if (req.http.Cookie == "") {
remove req.http.Cookie;
}
}
if (req.http.X-Forwarded-Proto == "https" ) {
set req.http.X-Forwarded-Port = "443";
} else {
set req.http.X-Forwarded-Port = "80";
set req.http.X-Forwarded-Proto = "http";
}
# Search in cache
if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|xml)$") {
set req.http.user-agent = "Mozilla";
unset req.http.Cookie;
return(lookup);
}
# Normalize Accept-Encoding to reduce vary
if (req.http.Accept-Encoding) {
if (req.http.User-Agent ~ "MSIE 6") {
unset 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 {
unset req.http.Accept-Encoding;
}
}
# Not cacheable by default
if (req.http.Authorization /* || req.http.Cookie */) {
return (pass);
}
return(lookup);
}
# vcl_fetch is called after a document has been successfully retrieved from the backend. Normal tasks her are to alter the response headers, trigger ESI processing, try alternate backend servers in case the request failed.
# In vcl_fetch you still have the request object, req, available. There is also a backend response, beresp. beresp will contain the HTTP headers from the backend.
sub vcl_fetch {
# Serve objects up to 2 minutes
set beresp.grace = 2m;
if (req.url ~ "\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|xml)$") {
unset beresp.http.set-cookie;
}
return (deliver);
}
sub vcl_deliver {
remove resp.http.X-Powered-By;
remove resp.http.Vary;
remove resp.http.X-Varnish;
remove resp.http.Via;
#remove resp.http.Age;
set resp.http.Server = "D2Server1.0" ;
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";
}
return (deliver);
}
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_error {
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>D2 Server</h3>
<p>XID: "} + req.xid + {"</p>
</body>
</html>"};
return (deliver);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment