Friday, March 11, 2011

Caching WordPress with Varnish

WordPress is a fantastic application that lets you get a website up and running in a matter of minutes. Not only that, but you require absolutely no knowledge of how to code to do so. As with many web applications though, it needs a little help in getting it to perform better. Lots of these dynamic apps breaks caching by using lots of cookies and setting headers like Cache-Control and Pragma. For a better in-depth tutorial on caching, read this article.

Requirements



  • Web server running on a different port than the standard one (TCP/80)

  • Varnish installed


Configuration


The standard location for Varnish’s configuration file is located at/etc/varnish/default.vcl. You will find below the configuration I use on this blog. See the comments to get a better understanding of what’s what. Also, make sure varnish will listen on port TCP/80 (use the -a flag when you start the daemon).
# Send all requests to your webserver
backend default {
.host = "127.0.0.1";
.port = "8080";
}

sub vcl_recv {
# If we don't set this, in our webserver's logs, we'll get varnish's IP instead
# of the real client's IP
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For ", " client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
if (req.request != "GET" && req.request != "HEAD") {
return (pass);
}
# Unless we're in the admin/login section, remove all cookies
if (!(req.url ~ "wp-(login|admin)")) {
unset req.http.cookie;
}
return (lookup);
}

sub vcl_pipe {
return (pipe);
}

sub vcl_pass {
return (pass);
}

sub vcl_hash {
set req.hash += req.url;
if (req.http.host) {
set req.hash += req.http.host;
} else {
set req.hash += server.ip;
}
return (hash);
}

sub vcl_hit {
if (!obj.cacheable) {
return (pass);
}
return (deliver);
}

sub vcl_miss {
return (fetch);
}

sub vcl_fetch {
if (!beresp.cacheable) {
return (pass);
}
if (beresp.http.Set-Cookie) {
return (pass);
}
if (!(req.url ~ "wp-(login|admin)")) {
unset req.http.cookie;
}
return (deliver);
}

sub vcl_deliver {
# Remove bad headers
remove resp.http.X-Varnish;
remove resp.http.Via;
remove resp.http.Age;
remove resp.http.X-Powered-By;
remove resp.http.Cache-Control;
remove resp.http.Pragma;
return (deliver);
}

Restart Varnish to load the new configuration.

Testing the recipe


Open up varnishstat in your terminal. You should see some stats. Ctrl-F5 a few times your site in your favorite browser. That way, you’re telling your browser to bypass its own cache and ask the site for fresh items. The first time you access your site, you will get lots of misses, but after that, you should get lots of cache hits.
Hitrate ratio:        9        9        9
Hitrate avg:     0.7302   0.7302   0.7302
22         0.00         0.16 Client connections accepted
132         0.00         0.95 Client requests received
100         0.00         0.72 Cache hits
5         0.00         0.04 Cache hits for pass
25         0.00         0.18 Cache misses

As you can see, I’m getting about 4 times more cache hits than misses. This way, Varnish will server all the static items and will let your webserver take care of all the dynamic content. Hope this helps a bit.

 

1 comment:

  1. I not to mention my buddies were found to be reviewing the great guides located on
    the blog and so before long I had a horrible feeling I had not expressed respect to the site owner for them.
    These guys ended up so stimulated to learn all of them and have sincerely been loving these things.
    Appreciation for truly being very considerate and
    for selecting such impressive themes millions of individuals are really desperate to understand about.
    Our sincere apologies for not expressing gratitude to you
    sooner.

    ReplyDelete