Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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)
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);
}
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 ~ "^RU$") {
# Block Russia
error 403 "Forbidden";
}
}
@section-io-gists
section-io-gists / varnish-anonymous-wordpress.vcl
Created September 11, 2017 15:49
This Varnish configuration is meant for Wordpress sites where site users are always anonymous.
# Please note: There is an underlying default Varnish behavior that occurs after the VCL logic
# you see below. You can see the bultin code here
# https://github.com/varnishcache/varnish-cache/blob/5.1/bin/varnishd/builtin.vcl
#
# See the VCL chapters in the Users Guide at https://www.varnish-cache.org/docs/
# and http://varnish-cache.org/trac/wiki/VCLExamples for more examples.
# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;
@section-io-gists
section-io-gists / section-user-throttling.vcl
Last active April 23, 2018 02:14
Add fallback values to stdtime2integer functions.
#vcl 4.0;
# comment the following imports if imported elsewhere
import cookie;
import header;
import std;
import var;
sub vcl_synth {
if (resp.status == 837) {
@section-io-gists
section-io-gists / bareDomainRedirectVCL4.vcl
Last active April 20, 2021 20:45
Redirect from bare domain to www site in VCL4.0
sub vcl_recv {
if (req.http.host !~ "www") {
return (synth(850, "Moved Permanently"));
}
}
sub vcl_synth {
if(resp.status == 850) {
set resp.http.Location = "https://www." + req.http.host + req.url;
set resp.status = 301;
@section-io-gists
section-io-gists / bareDomainRedirectVCL3.vcl
Last active April 20, 2021 20:45
Redirect from bare domain to www site in VCL3
sub vcl_recv {
if (req.http.host !~ "www") {
error 850 "Moved Permanently";
}
}
sub vcl_error {
if(obj.status == 850) {
set obj.http.Location = "https://www." + req.http.host + req.url;
set obj.status = 301;
@section-io-gists
section-io-gists / language-header.vcl
Last active October 9, 2017 15:35
Language header based on country code.
sub vcl_recv {
# Set a X-Language header to spanish is the client is from a spanish speaking country
if (req.http.section-io-geo-country ~ "^(AR|BO|CL|CO|CR|CU|DO|EC|SV|GQ|GT|HN|MX|NI|PA|PY|PE|ES|UY|VE)$") {
set req.http.X-Language = "Spanish";
} else {
set req.http.X-Language = "English";
}
}
sub vcl_hash {