A Beginner’s Guide to Setting Up Varnish 6.0 for Web Performance Optimization behind a WordPress site.
Here is an example config to use behind a wordpress site and has do not cache if a user is logged with some hardning to not show what kind of server/version in http headers.
default.vcl
vcl 4.0;
backend default {
.host = "wordpress";
.port = "80";
}
sub vcl_recv {
if (req.url == "/varnish-healthcheck") {
return (synth(200, "OK"));
}
# Do not cache administrative pages
if (req.url ~ "^/(wp-admin|wp-login|login|wp-cron|wp-json)") {
return(pass);
}
# Exclude specific cookies from caching
if (req.http.Cookie ~ "wordpress_logged_in|comment_author") {
return(pass);
}
# Set cache bypass for POST requests
if (req.method == "POST") {
return(pass);
}
# Set cache bypass for certain request types
if (req.method == "PURGE" || req.method == "BAN") {
return (synth(405, "Method not allowed"));
}
# Set default cache lifetime for other requests
set req.http.Cache-Control = "max-age=3600";
# Set a grace period for serving stale content
set req.grace = 600s;
unset req.http.Cookie;
}
sub vcl_deliver {
# Hardning - do not show version in headers.
unset resp.http.Via;
unset resp.http.Server;
unset resp.http.x-powered-by;
if (obj.hits > 0) { # Add debug header to see if it's a HIT/MISS and the number of hits, disable when not needed
set resp.http.x-varnish-cache-hit = "hit";
} else {
set resp.http.x-varnish-cache-hit = "miss";
}
# Please note that obj.hits behaviour changed in 4.0, now it counts per objecthead, not per object
# and obj.hits may not be reset in some cases where bans are in use. See bug 1492 for details.
# So take hits with a grain of salt
set resp.http.x-varnish-cache-hits = obj.hits;
}
sub vcl_backend_response {
# Setting http header on all request.
set beresp.http.x-varnish-x = "varnish";
# Cache 200 responses for 10 minutes
if (beresp.status == 200) {
set beresp.ttl = 600s;
set beresp.http.x-varnish-cache-hit = "hit";
}
# Add custom header indicating cache hit or miss
if (beresp.status == 200 && beresp.http.Set-Cookie == "") {
} else {
set beresp.http.x-varnish-cache-hit = "miss";
}
}
Varnish is a powerful caching system that allows developers to configure their web servers for efficient performance optimization. One of its most important features is its administrative interface called ‘varnishadm’, which provides a command-line environment where users can interact with the Varnish cache directly. Here are some examples of commands you might use frequently using varnishadm
Here are some commonly used commands in varnishadm
, along with a brief description for each command:
ban
: This command is used to remove objects from cache based on specific patterns.
varnishadm "ban req.url ~ /some/path"
list
: This command is used to list various details about the Varnish cache. Here are some examples
# List all caches:
varnishadm 'list'
# List specific cache (e.g., ‘VBE_HIT’):
varnishadm 'list VBE_HIT'
show parameters
: This command displays the current configuration of the Varnish cache.bashCopy codevarnishadm show parameters
shutdown
: This command is used to shut down a running instance of Varnish. It’s important to note that this will cause all ongoing sessions to be lost, so it should only be done when necessary and ideally in response to an error or other critical situation.bashCopy codevarnishadm shutdown
storage.free
: This command returns the amount of free storage space available on Varnish’s backend storage.bashCopy codevarnishadm 'storage.free'
stat
: This command is used to display various statistics about Varnish’s operation, such as cache hit and miss rates.bashCopy codevarnishadm 'stat'
ping
: This command checks if thevarnishadm
interface is active and responsive. It returns “PONG” if successful.bashCopy codevarnishadm ping
Remember that these are just some of the commands you might use with varnishadm
, and there are many more commands available for specific tasks or debugging purposes. Always refer to the official Varnish documentation for a comprehensive list and detailed explanations of all commands.