Introduction

As www.devquotes.com was suffuring for a huge peak of traffic, I figured I should install some caching server to meet the performance requirements.

I knew that Varnish was a good choice because of the many articles I came into the last few years, so I decided to give it a try.

Installation

This server is running Ubuntu 11.04, so installing Varnish  itself was kinda easy:

$ sudo apt-get install varnish

Varnish configuration

For god knows what reason (which is probably right), the Varnish packagers decided that the default configuration should prevent Varnish from starting. Well, OK. Basically, we’ll need to edit 2 files:

  • /etc/varnish/default.vcl Caching rules
  • /etc/default/varnish Varnish daemon configuration

First, let’s start with the Varnish configuration. I changed the following:

# Should we start varnishd at boot? Set to "yes" to enable.
START=yes
...
## Alternative 2, Configuration with VCL
#
# Listen on port 6081, administration on localhost:6082, and forward to
# one content server selected by the vcl file, based on the request. Use a 1GB
# fixed-size cache file.
#
DAEMON_OPTS="-a :80 \
 -T localhost:6082 \
 -f /etc/varnish/default.vcl \
 -S /etc/varnish/secret \
 -s file,/var/lib/varnish/$INSTANCE/varnish_storage.bin,1G"

I changed the Varnish listening port from 6081 to 80.

Those changes will allow Varnish to start, as long as you don’t have Apache running on the same machine, in which case the port 80 would already be open, and thus prevent Varnish from starting.

Second, I edited the VCL caching rules:

# Drop any cookies sent to WordPress.
sub vcl_recv {
  if (!(req.url ~ "wp-(login|admin)")) {
    if (!(req.url ~ "(preview).*(preview_id)")) {
      unset req.http.cookie;
    }
  }
}
# Drop any cookies WordPress tries to send back to the client.
sub vcl_fetch {
  if (!(req.url ~ "wp-(login|admin)")) {
    unset beresp.http.set-cookie;
  }
}

This is mostly a copypasta from the official Varnish website, I only added the condition on « preview » so admins can still preview their posts.

Apache configuration

I edited my Apache configuration with the following:

NameVirtualHost *:8080
Listen 8080

And I changed the port on the devquotes.com virtual host to 8080 aswell, then I restarted Apache:

$ sudo /etc/init.d/apache2 restart

Conclusion

Boom! The website was running again, with the best latency it ever has.

Varnish handled peaks around 40 requests/second without any problems. From what I understand we could even handle WAY more, at least 1000 requests/sec.

I will update this post as soon as I rewrite the caching rules for better handling of WordPress. I also heard about a nice Varnish HTTP Purge WordPress plugin that may help us a little bit.

Trackback

only 1 comment untill now

  1. A good start… and by far the best way to accelerate WordPress and most CMS’s… I was getting 1600 reqs/sec on a single core Centos VM with 1 GB Ram :)

Add your comment now

CommentLuv badge