Skip to content

Instantly share code, notes, and snippets.

@IchHabRecht
Created August 21, 2013 20:58
Show Gist options
  • Save IchHabRecht/6300193 to your computer and use it in GitHub Desktop.
Save IchHabRecht/6300193 to your computer and use it in GitHub Desktop.
[VCL] Advanced Varnish-TYPO3 Configuration
backend default {
.host = "127.0.0.1";
.port = "8080";
}
# Enable flushing access only to internals
acl flushers {
"127.0.0.1";
}
sub vcl_recv {
# Set backend depending on host
set req.backend = default;
# Set a unique cache header with client ip
if (req.restarts == 0) {
if (client.ip != "127.0.0.1") {
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;
}
}
}
# Allow the backend to deliver old content up to 1 day
set req.grace = 24h;
# Only allow known requests
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 neither GET nor HEAD request, send to backend but do not cache
# This means that POST requests are not cached
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
# Strip hash as the server doesn't need it
if (req.url ~ "\#") {
set req.url = regsub(req.url, "\#.*$", "");
}
# Always cache the following file types for all users
if (req.url ~ "\.(png|gif|jpeg|jpg|ico|swf|css|js|pdf|txt)(\?|$)") {
unset req.http.Cookie;
}
# If any authorisation was set do not cache
if (req.http.Authorization || req.http.Cookie ~ "fe_typo_user") {
return (pass);
}
# If we work in backend don't cache anything
if (req.http.Cookie ~ "be_typo_user") {
return (pass);
} else {
# Pass all no_cache=1 sites and eID scripts
if (req.url ~ "(\?|&)no_cache=1" || req.url ~ "(\?|&)eID=") {
return (pass);
}
# Delete cookies
unset req.http.Cookie;
}
# Handle compression correctly. Different browsers send different
# "Accept-Encoding" headers, even though they mostly all support the same
# compression mechanisms.
if (req.http.Accept-Encoding) {
if (req.http.Accept-Encoding ~ "gzip") {
# If the browser supports gzip, that is what we use
set req.http.Accept-Encoding = "gzip";
} else if (req.http.Accept-Encoding ~ "deflate") {
# Next try deflate encoding
set req.http.Accept-Encoding = "deflate";
} else {
# Unknown algorithm. Remove it and send unencoded.
unset req.http.Accept-Encoding;
}
}
# Lookup in cache
return (lookup);
}
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_fetch {
# Set default cache to 24 hours
set beresp.ttl = 24h;
# Deliver old content up to 1 day
set beresp.grace = 24h;
# Set cache for 3 days
if (req.url ~ "\.(png|gif|jpeg|jpg|ico|swf|css|js|pdf|txt)(\?|$)") {
set beresp.ttl = 72h;
}
# Delete cookie
if (req.request == "POST" || req.url ~ "^/typo3" || req.url ~ "(\?|&)eID=") {
} else {
unset beresp.http.Set-Cookie;
}
if (beresp.ttl <= 0s || beresp.http.Set-Cookie || beresp.http.Vary == "*") {
return (hit_for_pass);
}
# Add additional information for backend server
if (client.ip ~ flushers) {
set beresp.http.X-TTL = beresp.ttl;
set beresp.http.X-Grace = beresp.grace;
}
return (deliver);
}
sub vcl_deliver {
if (client.ip !~ flushers) {
# Remove some significant headers
unset resp.http.X-Varnish;
unset resp.http.Via;
unset resp.http.Age;
unset resp.http.X-Powered-By;
} else {
# Add additional information for backend server
if (obj.hits > 0) {
set resp.http.X-Cache = "Hit (" + obj.hits + ")";
} else {
set resp.http.X-Cache = "Miss";
}
}
return (deliver);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment