I spent about an hour yesterday debugging a Varnish page caching issue. I combed the site configuration and code for anything that might be setting
0 (effectively disabling caching), I checked and re-checked the
/admin/config/development/performance settings, verifying the 'Expiration of cached pages' (
page_cache_maximum_age) had a non-zero value and that the 'Cache pages for anonymous users' checkbox was checked.
After scratching my head a while, I realized that the headers I was seeing when using
curl --head [url] were specified as the defaults in drupal_page_header(), and were triggered any time there was a message displayed on the page (e.g. via
X-Drupal-Cache: MISS<br />
Expires: Sun, 19 Nov 1978 05:00:00 GMT<br />
Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0<br />
X-Content-Type-Options: nosniff<br />
On this particular site, the
error_level was set to
1 to show all errors on the screen, and the page in question had a PHP error displayed on every page load.
0 ('None' on the
/admin/config/development/logging page), Drupal sent the correct cache headers, Varnish was able to cache the page, and my sanity was restored.
Kudos especially to this post on coderwall, which jogged my memory.
Other potential reasons a page might not be showing as cacheable:
- A form with a unique per-user token may be present.
- An authenticated user is viewing the page (Drupal by default marks any page view with a valid session as no-cache).
- Someone set
\Drupal::service('page_cache_kill_switch')->trigger();(Drupal 8), or
- Some configuration file that's being included is either setting