My Varnish VCL for WordPress

On Varnish’s official website, there is a WordPress optimization guide For The Impatient: Preparing Varnish/Wordpress for a Slashdotting in 60 seconds or less….

The problem is that it removes cookie too aggressively. All non admin page will be virtually static. So I made my own vcl to remove cookies for only static files.

Here it is

backend default {
.host = "";
.port = "80";

sub vcl_recv {
# Normalize Content-Encoding
    if (req.http.Accept-Encoding) {
        if (req.url ~ ".(jpg|png|gif|gz|tgz|bz2|lzma|tbz)(?.*|)$") {
            remove req.http.Accept-Encoding;
        } elsif (req.http.Accept-Encoding ~ "gzip") {
            set req.http.Accept-Encoding = "gzip";
        } elsif (req.http.Accept-Encoding ~ "deflate") {
            set req.http.Accept-Encoding = "deflate";
        } else {
            remove req.http.Accept-Encoding;
# Remove cookies and query string for real static files
    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, "?.*$", "");
# Remove cookies from front page
    if (req.url ~ "^/$") {
       unset req.http.cookie;
sub vcl_fetch {
        if (req.url ~ "^/$") {
                unset obj.http.set-cookie;

So all interactive pages will be sent to php backend with correct cookies. All static files and front page will be served by varnish proxy.

  • Hi Yejun,

    I’m wondering if you have considered creating a plug-in to work with CloudFront as well? Please let us know if you are interested.

    We ran across Drupal support for a CloudFront module here: last week, so would love to see one for WordPress as well.



    • Yejun

      @[email protected]
      I will take a look at that drupal module to see what it actually does. And if you don’t know already there is one wordpress plugin which can do aws S3 offloading.
      I will get back to you laterly.

  • Vito Botta

    Hi, first of all many thanks. I am new to Varnish and will be looking to install it tonight for the WordPress blog I am preparing, so this comes in handy. I don’t know yet about the installation/configuration, however, would this solution be something that can be left in place all the time or would you advise to use it only when a spike in the traffic is foreseen / is happening?
    Would there be any conflicts, or would it break something during normal use?
    Many thanks in advance.

  • Hmm that code gives me this error:

    Message from VCC-compiler:
    Regexp compilation error:

    empty (sub)expression

    (input Line 9 Pos 23)
    if (req.url ~ .(jpg|png|gif|gz|tgz|bz2|lzma|tbz)(?.*|)$) {
    Running VCC-compiler failed, exit 1
    VCL compilation failed

  • Hi
    I was looking for some fast guide on how i can install varnish on shared hosting like bluehost?I managed to install xcache so I’m interested if procedure is similar or there is more configuring. as you may assume i’m not an expert in this issues:)thanx in advance

  • The varnish worked like a charm. But i hat to set all my VirtualHost entries to port 8080 as well as the backend port in the vcl file. I use a file as cache store, because my VPS lacks a bit of memory.

    Anyways, thx a million for sharing your config file.

  • Pingback: Varnish + Lighttpd | Constant Shift()

  • Pingback: Wordpress: Performancetuning –

  • Hi

    A proper WordPress plugin for Varnish would be a *very* nice thing. The expire link in is the biggest issue I see with getting it to work properly. The other thing that would be nice is a cache pre-load for the stuff you want to stay fresh in the cache (front page etc).


  • Anonymous

    Hey, I wrote a new Varnish VCL that supports WordPress in a lot more depth:  

    Load balancingProbingDoes not cache wp-adminPuts all uploads/content requests onto one serverPurgingLong timeout for file uploadsXML RPC supportCustom 404 and 500 messageForwards user IP address for commentsWordPress Varnish VCL

  • John

    Thanks for that. I am using it. If you upgrade to Varnish 3 have to change the syintax as per