Home > Web > My Varnish VCL for WordPress

My Varnish VCL for WordPress

April 5th, 2009

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 = "10.25.0.1";
.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.

Bookmark and Share  
 

Yejun Web , ,

  1. April 7th, 2009 at 00:02 | #1

    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: http://drupal.org/project/cloudfront last week, so would love to see one for WordPress as well.

    Regards,

    -Tal

  2. April 7th, 2009 at 10:20 | #2

    @Tal@AWS
    Hi,
    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.
    -Yejun

  3. Vito Botta
    May 6th, 2009 at 06:50 | #3

    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.

  4. November 3rd, 2009 at 10:27 | #4

    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

  5. November 18th, 2009 at 09:20 | #5

    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

  6. December 1st, 2009 at 15:56 | #6

    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.

  7. November 2nd, 2010 at 19:40 | #7

    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).

    Bob

  1. January 24th, 2010 at 13:19 | #1
  2. January 29th, 2010 at 16:49 | #2