Skip to content

Instantly share code, notes, and snippets.

@ieatkillerbees
Last active August 29, 2015 14:18
Show Gist options
  • Save ieatkillerbees/48be2f00df83e162eba1 to your computer and use it in GitHub Desktop.
Save ieatkillerbees/48be2f00df83e162eba1 to your computer and use it in GitHub Desktop.
Example VCL for Supercharging Content Delivery presentation
#
# 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.
# Marker to tell the VCL compiler that this VCL has been adapted to the
# new 4.0 format.
vcl 4.0;
import directors;
# Health check
probe health_check {
.url = "/";
.timeout = 1s;
.interval = 5s;
}
acl purge {
"127.0.0.1";
"45.55.163.206";
}
# Default backend definition. Set this to point to your content server.
backend server1 {
.host = "45.55.140.8";
.probe = health_check;
}
backend server2 {
.host = "45.55.163.196";
.probe = health_check;
}
backend server3 {
.host = "45.55.163.193";
.probe = health_check;
}
sub vcl_init {
new vdir = directors.round_robin();
vdir.add_backend(server1);
vdir.add_backend(server2);
vdir.add_backend(server3);
}
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 == "PURGE") {
if (!client.ip ~ purge) {
return(synth(403,"Forbidden"));
}
return(purge);
}
set req.backend_hint = vdir.backend();
if (req.url ~ "wp-admin|wp-login") {
return (pass);
}
unset req.http.cookie;
}
sub vcl_backend_response {
# Happens after we have read the response headers from the backend.
#
# Here you clean the response headers, removing silly Set-Cookie headers
# and other mistakes your backend does.
if (bereq.url ~ ".png$|.gif$|.jpg$") {
set beresp.ttl = 8h;
}
if (!beresp.http.Cache-Control) {
set beresp.ttl = 5m;
}
if (beresp.status == 200 && beresp.http.content-length == "0") {
return(retry);
}
set beresp.grace = 1h;
}
sub vcl_deliver {
# Happens when we have all the pieces we need, and are about to send the
# response to the client.
#
# You can do accounting or modifying the final object here.
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment