title | description | tags | categories | |
---|---|---|---|---|
Bypassing Cache with HTTP Headers |
Set HTTP headers to disable caching along Pantheon's edge layer, Varnish. |
|
You can use a variety of mechanisms to determine which responses from your Drupal or WordPress site should be excluded from caching. Ultimately, these mechanisms result in setting HTTP headers that signal cacheability to Varnish and recipients of the response, like a browser.
[Agency DevOps Training](https://pantheon.io/agencies/learn-pantheon?docs){.external}
Learn industry best practices for caching, how to take advantage of them on the platform, and troubleshooting common issues with help from the experts at Pantheon.
Some web developers choose to aggregate all of their caching logic in one place, often the settings.php
file of Drupal or a plugin dedicated to site-specific functionality in WordPress (as shown in the examples below). Alternatively, you can spread out cache-related code so that it is closest to the elements (i.e. sidebars, footers) that cause the cacheability of the response to be limited (as in this Drupal 8 example).
// $build is a render array.
$build['#cache']['max-age'] = 0;
Drupal 8 will "bubble up" this information so that if any small block on a page requires a cache max age of zero, the entire page will be uncacheable. Currently Cache Control Override module is required for this feature to behave correctly.
/*
* Set $regex_path_patterns accordingly.
*
* We don't set this variable for you, so you must define it
* yourself per your specific use case before the following conditional.
*
* For example, to exclude pages in the /news/ and /about/ path from cache, set:
*
* $regex_path_patterns = array(
* '#^/news/?#',
* '#^/about/?#',
* );
*/
$regex_path_patterns = array(
'#^/news/?#',
'#^/about/?#',
);
// Loop through the patterns.
foreach ($regex_path_patterns as $regex_path_pattern) {
if (preg_match($regex_path_pattern, $_SERVER['REQUEST_URI'])) {
drupal_page_is_cacheable(FALSE);
$conf['page_cache_maximum_age'] = 0;
// No need to continue the loop once there's a match.
break;
}
}
send_headers
. This will override `max-age` configured within the Pantheon Cache plugin for all matching requests:
/*
* Set $regex_path_patterns accordingly.
*
* We don't set this variable for you, so you must define it
* yourself per your specific use case before the following conditional.
*
* For example, to exclude pages in the /news/ and /about/ path from cache, set:
*
* $regex_path_patterns = array(
* '#^/news/?#',
* '#^/about/?#',
* );
*/
$regex_path_patterns = array(
'#^/news/?#',
'#^/about/?#',
);
// Loop through the patterns.
foreach ($regex_path_patterns as $regex_path_pattern) {
if (preg_match($regex_path_pattern, $_SERVER['REQUEST_URI'])) {
add_action( 'send_headers', 'add_header_nocache', 15 );
// No need to continue the loop once there's a match.
break;
}
}
function add_header_nocache() {
header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
}
As an alternative to using HTTP headers to control downstream caching, you can set a NO_CACHE
cookie. For details, see Working with Cookies on Pantheon.
Pantheon does not support manually editing and updating the VCL. We use a standard VCL for all sites on the platform. Requests are accepted, but we do not guarantee change requests will be implemented.
To test whether or not a page is being served from Pantheon's edge caching layer, examine the headers output (Age
, Cache-Control
, Set-Cookie
) via the following curl command:
$ curl -I dev.mysite.com
HTTP/1.1 200 OK
X-Pantheon-Styx-Hostname: styx1a
server: nginx/1.0.15
content-type: text/html; charset=utf-8
x-drupal-cache: MISS
set-cookie: SESSf60876d132c0913e5fc728eec7f71e38=M1Sr0bxLbbgYmbg1EW7N8sGF4anrKP1np25EkYta-ZU; expires=Wed, 19-Dec-2012 22:04:58 GMT; path=/; domain=.dev.mysite.com; HttpOnly
Cache-Control: no-cache, must-revalidate, max-age=0
last-modified: Mon, 26 Nov 2012 18:31:30 +0000
expires: Sun, 19 Nov 1978 05:00:00 GMT
x-pantheon-endpoint: c18646dd-aa2b-4faa-a4e3-d71ec3a5ce43
Date: Mon, 26 Nov 2012 18:31:38 GMT
X-Varnish: 486741958
Age: 0
Via: 1.1 varnish
Connection: keep-alive
X-Pantheon-Edge-Server: 108.166.58.245
Vary: Accept-Encoding, Cookie
The Cache-Control
header in this example instructs Pantheon's edge caching layer (Varnish) not to cache the response for this request. If you run the command again, you should continue to see Age: 0
for excluded pages. For more details, see Testing Global CDN Caching.