Getting WordPress to work with Varnish 4.0

  • Post author:
  • Post last modified:November 14, 2024
  • Reading time:1 mins read

With Varnish 4.0, the VCL has changed. So an update from Varnish 3 .0 to Varnish 4.0 requires a change in the VCL file, /etc/varnish/default.vcl. As a part of Varnish cache server configuration for a WordPress based site, using nginx web server, the following VCL is required in /etc/varnish/default.vcl for dropping cookies sent to WordPress and also for dropping any cookies that WordPress tries to send back to the client.

vcl 4.0;

# Default backend definition. Set this to point to your content server.
backend default {
    .host = "127.0.0.1";
    .port = "8080";
}

# Drop any cookies sent to WordPress.
sub vcl_recv {
        if (!(req.url ~ "wp-(login|admin)")) {
                unset req.http.cookie;
        }
}

# Drop any cookies WordPress tries to send back to the client.
sub vcl_backend_response {
        if (!(bereq.url ~ "wp-(login|admin)")) {
                unset beresp.http.set-cookie;
        }
}

Karunesh Johri

Software developer, working with C and Linux.