Skip to content

Instantly share code, notes, and snippets.

@section-io-gists
Last active July 20, 2017 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save section-io-gists/c4d99b0bfad316f654a85d04e4e9ed9d to your computer and use it in GitHub Desktop.
Save section-io-gists/c4d99b0bfad316f654a85d04e4e9ed9d to your computer and use it in GitHub Desktop.
sub vcl_recv {
# Happens before we check if we have this in cache already.
#
# Typically you clean up the request here, removing cookies you don't need,
# rewriting the request, etc.
if (req.method != "GET" && req.method != "HEAD" && req.method != "PURGE") {
return (pass);
}
return(hash);
}
sub vcl_backend_response {
if (beresp.http.Content-Type ~ "text/html" && beresp.status < 400) {
# You can set exclusive caching rules by caching everything that does not match certain URL patterns.
# Note, you should not use both exclusive caching and inclusive caching together, choose one.
if (bereq.url !~ "admin" || bereq.url !~ "checkout") {
# Cache HTML pages for 1 hour.
unset beresp.http.Set-Cookie;
set beresp.ttl = 60m;
set beresp.grace = 120m;
return(deliver);
}
# Alternatively you can use inclusive logic to only cache specific URLs.
# This example caches the products and category pages.
# if (bereq.url ~ "\products" || bereq.url ~ "\category") {
# Cache HTML pages for 1 hour.
# unset beresp.http.Set-Cookie;
# set beresp.ttl = 60m;
# set beresp.grace = 120m;
# return(deliver);
#}
}
#default to mark everything else uncacheable
set beresp.uncacheable = true;
set beresp.ttl = 120s;
return(deliver);
}
sub vcl_deliver {
# section.io default code
#
# Purpose: We are setting 'HIT' or 'MISS' as a custom header for easy debugging.
if (resp.http.X-Varnish ~ "\d+\s\d+") {
set resp.http.section-io-cache = "Hit";
} else {
set resp.http.section-io-cache = "Miss";
}
}
sub vcl_hash {
# section.io default code
#
# Purpose: Split cache by HTTP and HTTPS protocol.
hash_data(req.http.X-Forwarded-Proto);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment