3

I'm trying to do a little test, so I installed Drupal7 w/ following modules (expire, boost, varnish, purge, rules and token). I'm usingvarnish-3.0.4-1.el5.centos.x86_64 in front of httpd-2.2.15-29.sl6.x86_64.

I added following into my settings.php:

// Add Varnish as the page cache handler.
$conf['cache_backends'] = array('sites/all/modules/varnish/varnish.cache.inc');
$conf['cache_class_cache_page'] = 'VarnishCache';
// Drupal 7 does not cache pages when we invoke hooks during bootstrap. This needs
// to be disabled.
$conf['page_cache_invoke_hooks'] = FALSE;

and I set following settings for varnish module inside of Drupal7:

# drush -r /usr/share/drupal7/ -l XXXXX vget varnish
varnish_bantype: "1"
varnish_cache_clear: "2"
varnish_control_key: "XXXXX"
varnish_control_terminal: "XXX.XX.XXX.XX:6082"
varnish_flush_cron: "0"
varnish_socket_timeout: 100
varnish_version: "3"
# 

yet I keep getting MISS

# curl -I XXXXX
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Scientific Linux)
X-Powered-By: PHP/5.3.3
Expires: Sun, 19 Nov 1978 05:00:00 GMT
Last-Modified: Thu, 24 Oct 2013 14:42:21 +0000
Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0
ETag: "1382625741"
Content-Language: en
X-Generator: Drupal 7 (http://drupal.org)
Content-Type: text/html; charset=utf-8
x-url: /
x-host: XXXXX
Accept-Ranges: bytes
Date: Thu, 24 Oct 2013 14:42:21 GMT
X-Varnish: 1527289503
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Served-By: XXXXX
X-Cache: MISS

#

even though it seems like if I go to Configuration->Performance and then enable Caching, I'd end up with a HIT, yet Status Report would say following message (after enabling Caching) Boost will not function properly while Drupal core cache is enabled. Disable Boost or the core cache.

* UPDATE *

this is after enabling boost, as you see Cache-Control: IS in there, yet still getting MISS from Varnish(

$ curl -I http://XXXXX/node/1
HTTP/1.1 200 OK
Server: Apache/2.2.15 (Scientific Linux)
Last-Modified: Mon, 28 Oct 2013 15:53:48 GMT
ETag: "802449-1da7-4e9cf1b11461e"
Accept-Ranges: bytes
Content-Length: 7591
Cache-Control: max-age=1209600
Expires: Mon, 11 Nov 2013 15:55:46 GMT
Content-Type: text/html; charset=UTF-8
x-url: /node/1
x-host: XXXXX
Accept-Ranges: bytes
Date: Mon, 28 Oct 2013 15:55:46 GMT
X-Varnish: 1527308733
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Served-By: XXX.XXXXX.XXX
X-Cache: MISS

$ 

What am I doing wrong?

7

Pay attention to the Cache-Control header returned from Drupal:

Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0

If Cache-Control is set to no-cache, Varnish will respect it by default. Either you must force caching from within Varnish in vcl_fetch or (much better), make Drupal deliver the proper header (Cache-Control: public, max-age=X). The simplest way to achieve the latter is to enable "Cache pages for anonymous users" in Administration » Configuration » Development and also set "Expiration of cached pages". You can hard-code those settings using:

$conf['cache'] = 1;
$conf['page_cache_maximum_age'] = 3600; // 1h

You also may force the Cache-Control header by adding it explicitely using a custom module by implementing hook_exit. A reasonable save implementation looks something like this:

/**
 * Implements hook_exit().
 *
 * @see drupal_serve_page_from_cache()
 */
function MYMODULE_exit($destination = NULL) {
  // If the client sent a session cookie, a cached copy will only be served
  // to that one particular client due to Vary: Cookie. Thus, do not set
  // max-age > 0, allowing the page to be cached by external proxies, when a
  // session cookie is present unless the Vary header has been replaced or
  // unset in hook_boot().
  $vary_header = drupal_get_http_header('vary');
  $max_age = !isset($_COOKIE[session_name()]) || isset($vary_header) ? variable_get('page_cache_maximum_age', 0) : 0;

  if (drupal_page_is_cacheable() && $max_age && $destination == NULL && !headers_sent()) {
    drupal_add_http_header('Cache-Control', 'public, max-age=' . $max_age);
  }
}

Also note that due to the fact that boost will also add a Cache-Control header, you still need to introduce some VCL logic in vcl_fetch. The following fragment will force Varnish to still store pages in the cache, even when they were delivered using boost with Cache-Control no-cache:

sub vcl_fetch {
  if (beresp.http.X-Cached-By ~ "Boost") {
    set beresp.ttl = 1 h;
  }
}
  • OK, yes I was looking at Cache-Control myself and I also saw that I forgot to enable Cache pages for anonymous users and set Expiration of cached pages, thank you for pointing it out for me) but because I use boost module, under Status Report I'm getting following message (after enabling) Boost will not function properly while Drupal core cache is enabled. Disable Boost or the core cache. – alexus Oct 24 '13 at 15:11
  • @alexus: You may also add the Cache-Control header using a custom module. See updated answer. – znerol Oct 24 '13 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.