Set max-age headers for specific pages, files, or paths

Varnish

Sometimes you may want to set specific max-age headers on a page-by-page basis. For example, a website may consist mainly of static pages, but has a news section that updates frequently. It makes sense to cache the static content for a day or more, but to give the news section a brief cache lifetime that's measured in minutes.

You can use either of the following methods, based on your installed version of Drupal:

Modules

These are some modules that can help set max-age headers.

The Context HTTP Headers module can provide this control. However, this module has a dependency on the Context module, which can add more overhead than you want to your website.

The Cache Control module provides you with the additional ability to set max-age headers on a page-by-page basis.

The Max age module allows you to control cache ages per content type.

Direct file changes, for files

In Drupal 7, you can change the Varnish cache for static items like files by adding a custom .htaccess file to any desired folders. It's strongly advised that you do not turn off caching entirely; instead modify the cache to something more appropriate for the website.

For example, placing this single-line .htaccess file inside a folder will tell external caches (like your browser's cache or Varnish) to cache all files in that directory and any subdirectories for 30 seconds:


# Cache all files on this folder at most for 30 seconds.
Header set Cache-Control "max-age=30, public"

Set this number to 0 to turn off caching entirely in this folder, but this is not recommended. Again, simply create an .htaccess file in the directory where the files are.

If you want to exclude a file type from Varnish entirely, this example prevents CSV or XML files from being cached.


# Prevent caching of CSV and XML files
<FilesMatch "\.(csv|xml)$">
Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"
</FilesMatch>

More information on bypassing the Varnish cache is available in the Bypassing the Varnish cache Help Center article.

Direct file changes, for paths

You can do this by altering the max-age headers, which you can control by overriding $conf['page_cache_maximum_age']. You can do this in the settings.php file.

You normally set this variable using the UI by navigating to Admin > Configuration > Development > Performance on Drupal 7 and entering a value in the Expiration of cached pages field.

The following code snippets change the max age time (in seconds). Use the snippet best suited for your website:

# Set max-age for /path/to/page
if (0 === strpos($_SERVER['REQUEST_URI'], '/path/to/page') { 
  $conf['page_cache_maximum_age'] = 300;
}

# Using SCRIPT_URL might be better since it ignores query string
if ($_SERVER['SCRIPT_URL'] == '/path/to/page') {
  $conf['page_cache_maximum_age'] = 300;
}

# Match all /blog* pages
if (isset($_GET['q']) && strpos($_GET['q'], 'blog') === 0) {
  $conf['page_cache_maximum_age'] = 300;
}

You can also use the .htaccess file for paths. This snippet ensures that anything in the sites/default/files/js path will have a max-age of 100 seconds or whatever value you place in the Header set. You could use this to allow some resources to have an expiration date in the far future, or a date significantly shorter than the default two week Drupal expiration, which is set like this:


# Cache all files for 2 weeks after access
ExpiresDefault A1209600
  SetEnvIf Request_URI "^/sites/default/files/js" LONG_CACHE=1
  Header set Cache-Control "max-age=100" env=LONG_CACHE

If, after completing the changes, you need to purge the path from the cache, you can manually purge a page from Acquia Cloud Varnish cache.

Contact supportStill need assistance? Contact Acquia Support