Skip to content

Instantly share code, notes, and snippets.

@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 / Varnish 4.0: Add HTTP response headers to indicate cache hit or miss
Last active March 5, 2022 14:29
Varnish 4.0: Add HTTP response headers to indicate cache hit/miss
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
@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 / 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 / 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 / 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 / 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 / basicAuthVCL4.vcl
Last active June 2, 2020 01:50
basicAuthVCL4.vcl
sub vcl_recv {
if (! req.http.Authorization ~ "Basic dXNlcm5hbWU6cGFzc3dvcmQ=") {
# This is checking for base64 encoded username:password combination
return(synth(401, "Authentication required"));
}
unset req.http.Authorization;
}
{
"property-id": "<insert perperty ID>",
"secret-key": "<insert secret key>",
"monitor-only": true
}
@section-io-gists
section-io-gists / wordpressConfig.vcl
Last active January 13, 2020 13:50
A full Wordpress Varnish configuration to copy paste into section.io
# Ref: https://www.varnish-software.com/blog/step-step-speed-wordpress-varnish-software
# This is an example VCL file for Varnish.
#
# It does not do anything by default, delegating control to the
# builtin VCL. The builtin VCL is called when there is no explicit
# return statement.
#
# 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.