Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@section-io-gists
section-io-gists / cacheEverything.vcl
Created September 11, 2015 03:51
Cache both static and dynamic content (Great for sites with no personalisation)
#section.io VCL sample. Copy paste into your section.io account to implement instantly
#vcl_recv - copy this code into the section called sub vcl_recv
#Normalise Accept-Encoding
if (req.http.Accept-Encoding) {
if (req.http.Accept-Encoding ~ "gzip") {
set req.http.Accept-Encoding = "gzip";
} else if (req.http.Accept-Encoding ~ "deflate") {
set req.http.Accept-Encoding = "deflate";
@section-io-gists
section-io-gists / ISEPureVarnish
Last active September 23, 2015 04:42
section.io ISE template (for Magento)
# This is a basic VCL configuration file for varnish. See the vcl(7)
# man page for details on VCL syntax and semantics.
#
# Default backend definition. Set this to point to your content
# server.
#
backend default {
.host = "next-hop";
.port = "80";
.first_byte_timeout = 300s;
{
"proxychain": [
{
"name": "varnish",
"image": "varnish:4.0.3"
}
],
"environments": {
"Production": {
"origin": {
@section-io-gists
section-io-gists / gist:721981ee1462fad435ee2d303390f491
Created April 24, 2016 21:36
ModSecurity Rule Engine Initialization Change from DetectionOnly to On
# -- Rule engine initialization ----------------------------------------------
# Enable ModSecurity, attaching it to every transaction. Use detection
# only to start with, because that minimises the chances of post-installation
# disruption.
#
#SecRuleEngine DetectionOnly
SecRuleEngine On
@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);
}
@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 / 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 / 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 / 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 / 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;