Skip to content

Instantly share code, notes, and snippets.

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);
}
@section-io-gists
section-io-gists / HTMLStreaming.vcl
Last active March 25, 2019 15:00
HTML Streaming solution to cache the HEAD of HTML
#Example of HTML Streaming. This gist involves both Varnish and nginx LUA reverse proxies
#Varnish:
sub vcl_recv {
# Do not stream on non-get requests
if (req.method != "GET" && req.method != "HEAD" && req.method != "PURGE") {
return (pass);
}
# By defaul disable holepunch unless below cases fall through (do not modify this line)
@section-io-gists
section-io-gists / RemoveQueryString.vcl
Last active May 12, 2017 05:23
Varnish 4 - Strip browser side tracking script to improve cache hit rate
sub vcl_recv {
# Strip browser side tracking script to improve cache hit rate
if (req.url ~ "[?&](utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl|mc_cid|mc_eid)=") {
set req.url = regsuball(req.url, "(?:(\?)?|&)(?:utm_source|utm_medium|utm_campaign|gclid|cx|ie|cof|siteurl|mc_cid|mc_eid)=[^&]+", "\1");
set req.url = regsuball(req.url, "(?:(\?)&|\?$)", "\1");
}
}
@section-io-gists
section-io-gists / GEOIPRedirection.vcl
Created May 8, 2017 04:52
Redirect traffic based on GEO IP lookup (Varnish 4)
sub vcl_recv {
# section.io Edge node automatically performs GEO IP (and city) lookup on every request.
# We create a request header called "section-io-geo-country" that you can leverage
if (req.http.section-io-geo-country ~ "^(AU|NZ)$") {
# Australia and New Zealand
#return (synth(802, "https://www.domain.com.au/"));
} else if (req.http.section-io-geo-country ~ "^(US|CA|MX)$") {
# US, Canada and Mexico
#return (synth(802, "http://www.domain.com/"));
@section-io-gists
section-io-gists / ForceHTTPSVarnish3.vcl
Last active April 21, 2019 16:11
Force traffic to HTTPS in Varnish 3.x
sub vcl_recv {
if (req.http.X-Forwarded-Proto !~ "https") {
error 850 "Moved permenantly";
}
}
sub vcl_error {
if(obj.status == 850) {
set obj.http.Location = "https://" + req.http.host + req.url;
set obj.status = 301;
@section-io-gists
section-io-gists / ForceHTTPSVarnish4.vcl
Last active July 29, 2020 12:28
Varnish 4.x to force HTTPS
sub vcl_recv {
if (req.http.X-Forwarded-Proto !~ "https") {
return (synth(850, "Moved Permanently"));
}
}
sub vcl_synth {
if(resp.status == 850) {
set resp.http.Location = "https://" + req.http.host + req.url;
set resp.status = 301;
@section-io-gists
section-io-gists / CustomMaintenancePage.vcl
Last active August 29, 2023 07:49
Varnish 4 - Custom maintenance page
import std;
acl whitelist {
"123.123.123.123";
"216.3.128.12";
}
sub vcl_recv {
# If not a whitelisted IP, then display maintenance page. Requires std library.
if(std.ip(regsub(req.http.X-Forwarded-For, "[, ].*$", ""), client.ip) !~ whitelist) {
@section-io-gists
section-io-gists / pagespeed-requirement.vcl
Created March 15, 2017 15:29
Using the PageSpeed module alongside Varnish, you will need to configure Varnish to handle PageSpeed optimizations.
# Note: You will want to add the snippet: `include "pagespeed-requirement.vcl";` above your `vcl_recv` in the default.vcl file.
sub vcl_recv {
call pagespeed_capability_detection;
}
# Function derived from requirements here https://modpagespeed.com/doc/downstream-caching#ps-capabilitylist
# Additional detection logic for crawlers, tablet and mobile devices.
sub pagespeed_capability_detection {
if (req.http.User-Agent ~ "(?i)Chrome/[3][2-9]+\.|Chrome/[4-9][0-9]+\.|Chrome/[0-9]{3,}\.") {
@section-io-gists
section-io-gists / staticCachingWithVaryFix.vcl
Last active September 19, 2017 22:13
Cache Static Content and also fix the Vary response header (Commonly needed when origin is responding with: Vary: Accept-Encoding,User-Agent)
#Varnish 4 config below
sub vcl_recv {
#Add font files to be cached as static caching checkbox misses .woff
if (req.url ~ ".*\.(?:css|js|jpe?g|png|gif|ico|swf|woff)(?=\?|&|$)") {
return (hash);
}
}
@section-io-gists
section-io-gists / hashByProto.vcl
Created February 16, 2017 04:34
Split Varnish Cache by the request header X-Forwarded-Proto - Useful when the origin is doing GEO IP redirects or changing content according to protocol
sub vcl_hash {
#Vary cache by protocol type to avoid caching things like HTTP->HTTPS redirects and different versions of files
hash_data(req.http.X-Forwarded-Proto);
}