Install varnish:
apt-get install varnish
An example Varnish config file /etc/varnish/default.vcl
, taken from Daniel Miessler:
backend default {
.host = "localhost";
.port = "8080";
}
acl purge {
"localhost";
}
sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return(lookup);
}
if (req.url ~ "^/$") {
unset req.http.cookie;
}
}
sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
}
sub vcl_miss {
if (req.request == "PURGE") {
error 404 "Not in cache.";
}
if (!(req.url ~ "wp-(login|admin)")) {
unset req.http.cookie;
}
if (req.url ~ "^/[^?]+.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(?.|)$") {
unset req.http.cookie;
set req.url = regsub(req.url, "?.$", "");
}
if (req.url ~ "^/$") {
unset req.http.cookie;
}
}
sub vcl_fetch {
if (req.url ~ "^/$") {
unset beresp.http.set-cookie;
}
if (!(req.url ~ "wp-(login|admin)")) {
unset beresp.http.set-cookie;
}
}
Starting Varnish
By default Varnish listens on port 6081
and uses localhost:6082
as its administration. We want to use Varnish as a replacement for the default webserver, so we must change 6081
into 80
. Open /etc/default/varnish
, find DAEMON_OPTS
and make sure it is defined as:
DAEMON_OPTS="-a :80
-T localhost:6082
-f /etc/varnish/default.vcl
-S /etc/varnish/secret
-s malloc,256m"
Since Varnish
is going to run on port 80
, you need to change the nginx
configuration to run on a different port:
listen 8080;
Start Varnish
with the following command (also restart nginx
):
service varnish start
service nginx restart
Varnish for WordPress
Purge Varnish
Useful commands
varnishlog
varnishstat