Last week I got called into a firestorm to fix a set of machines that were having problems. As Varnish was in the mix, the first thing I noticed was the hit rate was extremely low as Varnish’s VCL wasn’t really configured well for WordPress. Since WordPress uses a lot of cookies and Varnish passes anything with a cookie to the backend, we have to know which cookies we can ignore so that we can get the cache hit rate up.
Obviously, static assets like javascript, css and images generally don’t need cookies, so, those make a good first target. Since some ad networks set their own cookies on the domain, we need to know which ones to set. However, to make a site resilient, we have to get a little more aggressive and tell Varnish to cache things against its judgement. When we do this, we don’t want to have surfers see stale content, so, we need to purge cached objects from Varnish when they are changed to keep the site interactive.
Caching is easy, purging is hard
This particular installation used W3 Total Cache, a plugin that does page caching, javascript/css minification and combining and handles a number of other features. I was unable to find any suggested VCL, but, several posts on the forums show a disinterest in supporting Varnish.
In most cases, once we determine what we’re caching, we need to figure out what to purge. When a surfer posts a comment, we need to clear the cached representation of that post, the Feed RSS and the front page of the site. This allows any post counters to be updated and keeps the RSS feed accurate.
W3TC includes the ability to purge, but, only works in a single server setting. If you put a domain name in the config box, it should work fine. If you put a series of IP addresses, your VCL either needs to override the hostname or, you need to apply the following patch. There are likely to be bugs, so, try this at your own risk.
If you aren’t using the Javascript/CSS Minification and combining or some of the CDN features that W3TC provides, then I would suggest WordPress-Varnish which is maintained by some people very close to the Varnish team.
I’ve maintained the original line of code from W3TC commented above any changes for reference.
--- w3-total-cache/inc/define.php 2011-06-21 23:22:54.000000000 -0400
+++ w3-total-cache-varnish/inc/define.php 2011-07-21 16:10:39.270111723 -0400
@@ -1406,11 +1406,15 @@
* @param boolean $check_status
* @return string
*/
-function w3_http_request($method, $url, $data = '', $auth = '', $check_status = true) {
+#cd34, 20110721, added $server IP for PURGE support
+# function w3_http_request($method, $url, $data = '', $auth = '', $check_status = true) {
+function w3_http_request($method, $url, $data = '', $auth = '', $check_status = true, $server = '') {
$status = 0;
$method = strtoupper($method);
- if (function_exists('curl_init')) {
+#cd34, 20110721, don't use CURL for purge
+# if (function_exists('curl_init')) {
+ if ( (function_exists('curl_init')) && ($method != 'PURGE') ) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
@@ -1474,7 +1478,13 @@
$errno = null;
$errstr = null;
- $fp = @fsockopen($host, $port, $errno, $errstr, 10);
+#cd34, 20110721, if method=PURGE, connect to $server, not $host
+# $fp = @fsockopen($host, $port, $errno, $errstr, 10);
+ if ( ($method == 'PURGE') && ($server != '') ) {
+ $fp = @fsockopen($server, $port, $errno, $errstr, 10);
+ } else {
+ $fp = @fsockopen($host, $port, $errno, $errstr, 10);
+ }
if (!$fp) {
return false;
@@ -1543,8 +1553,9 @@
* @param bool $check_status
* @return string
*/
-function w3_http_purge($url, $auth = '', $check_status = true) {
- return w3_http_request('PURGE', $url, null, $auth, $check_status);
+#cd34, 20110721, added server IP
+function w3_http_purge($url, $auth = '', $check_status = true, $server = '') {
+ return w3_http_request('PURGE', $url, null, $auth, $check_status, $server);
}
/**
diff -Naur w3-total-cache/lib/W3/PgCache.php w3-total-cache-varnish/lib/W3/PgCache.php
--- w3-total-cache/lib/W3/PgCache.php 2011-06-21 23:22:54.000000000 -0400
+++ w3-total-cache-varnish/lib/W3/PgCache.php 2011-07-21 16:04:07.247499682 -0400
@@ -693,7 +693,9 @@
$varnish =& W3_Varnish::instance();
foreach ($uris as $uri) {
- $varnish->purge($uri);
+#cd34, 20110721 Added $domain_url to build purge hostname
+# $varnish->purge($uri);
+ $varnish->purge($domain_url, $uri);
}
}
}
diff -Naur w3-total-cache/lib/W3/Varnish.php w3-total-cache-varnish/lib/W3/Varnish.php
--- w3-total-cache/lib/W3/Varnish.php 2011-06-21 23:22:54.000000000 -0400
+++ w3-total-cache-varnish/lib/W3/Varnish.php 2011-07-21 16:04:52.836919164 -0400
@@ -70,7 +70,7 @@
* @param string $uri
* @return boolean
*/
- function purge($uri) {
+ function purge($domain, $uri) {
@set_time_limit($this->_timeout);
if (strpos($uri, '/') !== 0) {
@@ -78,9 +78,11 @@
}
foreach ((array) $this->_servers as $server) {
- $url = sprintf('http://%s%s', $server, $uri);
+#cd34, 20110721, Replaced $server with $domain
+# $url = sprintf('http://%s%s', $server, $uri);
+ $url = sprintf('%s%s', $domain, $uri);
- $response = w3_http_purge($url, '', true);
+ $response = w3_http_purge($url, '', true, $server);
if ($this->_debug) {
$this->_log($url, ($response !== false ? 'OK' : 'Bad response code.'));
diff -Naur w3-total-cache/w3-total-cache.php w3-total-cache-varnish/w3-total-cache.php
--- w3-total-cache/w3-total-cache.php 2011-06-21 23:22:54.000000000 -0400
+++ w3-total-cache-varnish/w3-total-cache.php 2011-07-21 15:56:53.275922099 -0400
@@ -2,7 +2,7 @@
/*
Plugin Name: W3 Total Cache
Description: The highest rated and most complete WordPress performance plugin. Dramatically improve the speed and user experience of your site. Add browser, page, object and database caching as well as minify and content delivery network (CDN) to WordPress.
-Version: 0.9.2.3
+Version: 0.9.2.3.v
Plugin URI: http://www.w3-edge.com/wordpress-plugins/w3-total-cache/
Author: Frederick Townes
Author URI: http://www.linkedin.com/in/w3edge
@@ -47,4 +47,4 @@
require_once W3TC_LIB_W3_DIR . '/Plugin/TotalCache.php';
$w3_plugin_totalcache = & W3_Plugin_TotalCache::instance();
$w3_plugin_totalcache->run();
-}
\ No newline at end of file
+}