Skip to content

Instantly share code, notes, and snippets.

@jesslop
Created June 19, 2018 00:00
Show Gist options
  • Save jesslop/99722eb56914c69b602b96e0f20422f4 to your computer and use it in GitHub Desktop.
Save jesslop/99722eb56914c69b602b96e0f20422f4 to your computer and use it in GitHub Desktop.
Varnish 4.0 Simple Configuration
#
# 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;
# Default backend definition. Set this to point to your content server.
backend default {
.host = "localhost";
.port = "8080";
}
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.
set req.backend_hint = default;
unset req.http.Cookie;
return(hash);
}
sub vcl_miss {
# Just pass the request along to the subroutine which will fetch something from the backend.
return(fetch);
}
sub vcl_hit {
# Pass the request along to the subroutine which will deliver a result from the cache.
return(deliver);
}
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.
# Get the response. Set the cache lifetime of the response to 1 hour.
set beresp.ttl = 1h;
# Indicate that this response is cacheable. This is important.
set beresp.http.X-Cacheable = "YES";
# Now pass this backend response along to the cache to be stored and served.
return(deliver);
}
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.
return(deliver);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment