Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@matthewjackowski
Last active June 19, 2021 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save matthewjackowski/8e118f1f7e2aef5b5eb2a03631e734f7 to your computer and use it in GitHub Desktop.
Save matthewjackowski/8e118f1f7e2aef5b5eb2a03631e734f7 to your computer and use it in GitHub Desktop.
A basic vcl setup for edge caching
# A basic setup for the edge
# Strips cookies and un-needed params
# Does a few redirects
# Sets forwarded proxy headers
# Custom error page
vcl 4.0;
import std;
# Backend setup
backend default {
.host = "brilliantcoding.us";
.port = "80";
}
sub vcl_recv {
# Only a single backend
set req.backend_hint= default;
# Setting http headers for backend
set req.http.X-Forwarded-For = client.ip;
set req.http.X-Forwarded-Proto = "https";
# Unset headers that might cause us to cache duplicate infos
unset req.http.Accept-Language;
unset req.http.User-Agent;
unset req.http.Vary;
# Redirect www to non-www
if (req.http.host ~ "^www\.") {
set req.http.x-redir = regsub(req.http.host, "^www\.(.*)", "https://\1") + req.url;
return (synth(750, "Moved permanently"));
}
# Redirect http to https
if ( std.port(server.ip) == 80) {
set req.http.x-redir = "https://" + req.http.host + req.url;
return (synth(750, "Moved permanently"));
}
# Redirect mobile to homepage
if (req.url ~ "/mobile/$" || req.url ~ "/m/$" ) {
set req.url = "/";
set req.http.x-redir = "https://" + req.http.host + req.url;
return (synth(750, "Moved permanently"));
}
# Weird things bing looks for
if (req.url ~ "sitemap\.aspx") {
set req.url = "/sitemap_index.xml";
set req.http.x-redir = "https://" + req.http.host + req.url;
return (synth(750, "Moved permanently"));
}
if (req.url ~ "aboutus\.aspx$") {
set req.url = "/about/";
set req.http.x-redir = "https://" + req.http.host + req.url;
return (synth(750, "Moved permanently"));
}
# drop params from static asset
if (req.url ~ "\.(gif|jpg|jpeg|swf|ttf|css|js|flv|mp3|mp4|pdf|ico|png)(\?.*|)$") {
set req.url = regsub(req.url, "\?.*$", "");
}
# drop tracking params
if (req.url ~ "\?(utm_(campaign|medium|source|term)|adParams|client|cx|eid|fbid|null|feed|ref(id|src)?|v(er|iew))=") {
set req.url = regsub(req.url, "\?.*$", "");
}
unset req.http.cookie;
}
sub vcl_backend_response {
# retry a few times if backend is down
if (beresp.status == 503 && bereq.retries < 3 ) {
return(retry);
}
if (bereq.url ~ "\.(css|js|ttf)(\?.*|)$") {
unset beresp.http.cookie;
unset beresp.http.Set-Cookie;
set beresp.ttl = 1d;
unset beresp.http.Cache-Control;
}
if (beresp.ttl < 120s) {
unset beresp.http.cookie;
unset beresp.http.Set-Cookie;
set beresp.ttl = 120s;
unset beresp.http.Cache-Control;
}
# remove extra headers
unset beresp.http.X-Powered-By;
unset beresp.http.Server;
unset beresp.http.Via;
unset beresp.http.X-Pingback;
unset beresp.http.X-Varnish;
}
sub vcl_hash {
if ( req.http.X-Forwarded-Proto ) {
hash_data( req.http.X-Forwarded-Proto );
}
}
sub vcl_synth {
# redirect for http
if (resp.status == 750) {
set resp.status = 301;
set resp.http.Location = req.http.x-redir;
return(deliver);
}
# display custom error page if backend down
if (resp.status == 503) {
set resp.status = 404;
synthetic(std.fileread("/etc/varnish/error.html"));
return(deliver);
}
}
sub vcl_deliver {
# oh noes backend is down
if (resp.status == 503) {
return(restart);
}
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
set resp.http.Access-Control-Allow-Origin = "*";
}
sub vcl_hit {
}
sub vcl_miss {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment