I thought I’d put a quick post here in the hope that I save someone a bit of frustration.
I was testing a new Drupal 7 site and I noticed that while the static files (JS, CSS, images, etc.) were all showing the proper cache lifetime, the nodes themselves were being sent back with a max-age=0, causing a reverse proxy cache miss on eminently-cacheable content. A bit of digging turned up the culprit: the mostly-hidden variable ‘page_cache_invoke_hooks’.
When running Drupal 7 behind a Varnish reverse proxy cache, make sure to include the following lines (suitably edited) in your settings.php:
# Tell Drupal it's behind a proxy
$conf['reverse_proxy'] = TRUE;
# Tell Drupal what addresses the proxy server(s) use
$conf['reverse_proxy_addresses'] = array('127.0.0.1','192.168.0.2');
# Bypass Drupal bootstrap for anonymous users so that Drupal sets max-age > 0
$conf['page_cache_invoke_hooks'] = FALSE;
Without the last variable, anonymous users will get nodes/pages with the max-age=0 and the proxy won’t cache it. Setting it to false allows the node to be sent with the max-age set to the cache lifetime set in the admin UI.
Thank you your a live saver. Ive been pulling my hair for a couple of days.
Thank you thank u!!!!
Great tips, really!
What about these other settings I found in http://drupal.org/node/1215564
$conf['cache'] = 1;
$conf['cache_lifetime'] = 0;
$conf['page_cache_maximum_age'] = 21600;
Do I need to enable these?
Unless I’m totally mistaken, those variables are already set within the Drupal UI on the Performance page and aren’t required here. Setting them here would override the values stored in the UI.
Very helpful, this made a huge difference in my load time although I skipped the proxy variables and only added the bootstrap bypass. Thanks for this!
Just a follow-up to this post… this issue was resolved in Drupal 7.4, per http://drupal.org/node/804864#comment-4592950
$conf['page_cache_invoke_hooks'] no longer needs to be added to settings.php for the max-age to be set.
Thanks for the heads-up. I really need to find some time to do a couple of new posts – this stuff is getting pretty dated.
Enjoyed this article, this goes directly into my feedburner subscribes
Thank you so much for the post. Really helpful. Save my frustration. This should definitely be added to README of the module.