Configure Caching for Drupal 7 behind Cloudflare

Cloudflare ended support for Drupal 7 in 2016 so there is not a Drupal 7 module available for Cloudflare integration.  The university's instance of Cloudflare will respect all caching headers sent by the web server or CMS, and it will automatically not cache content or serve cached content if it sees that one of the standard PHP session cookies is set.

Cloudflare won't cache any content for any visitor that has a cookie set whose name matches the following pattern.  Cookies with these names are considered to be "login cookies" that indicate the presence of a user-specific session.

^(S?SESS|phpsessid=|wp-|wordpress|_shibsession_|mod_auth_openidc_session=|JSESSIONID=)

HTTP caching headers tell Cloudflare what pages and assets to cache (or not) and how long to cache.

Drupal configuration

For Drupal 7, the following configuration is recommended:

  • Set Cache-control: no-store for pages for logged in (authenticated) users

Configure the following, either by UI or command line:

  • UI:  In the Drupal 7 Administration UI, under Configuration → Development → Performance, configure:
    • Enable “Cache pages for anonymous users”
    • Set “Minimum cache lifetime” to “<none>”
    • Set “Expiration of cached pages” to “5 min” (or up to “15 min” depending on individual site needs)
  • Command line:  use drush on the command line to set variables:
    • drush vset cache 1
    • drush vset cache_lifetime 0
    • drush vset page_cache_maximum_age 300

Install & Configure Drupal CacheExclude Module

You may also consider installing the CacheExclude module for Drupal 7 to turn off caching set by Drupal for pages that require login so that one user doesn't get another authenticated user's content.  It also allows for configuration of node types that should not be cached.

Install the module. From the site's DRUPAL_ROOT directory:

wget -O - 'https://ftp.drupal.org/files/projects/cacheexclude-7.x-2.3.tar.gz' | tar -C `pwd`/sites/all/modules -xzf -

Enable the module:

drush en cacheexclude

Configure the module for your site by following the instructions in the Drupal 7 Administration UI, under Configuration → System → Cache exclusions for syntax for adding pages to exclude from caching.  Variable settings may also be set/viewed via drush.  For example, after adding the following two pages in the text area of the UI and saving the configuration:
user/login
supersecretfiles/*

Pages to exclude from caching, via drush:
drush vget cacheexclude_list
cacheexclude_list: "user/login\r\nsupersecretfiles/*"

Content types to exclude from caching, via drush:
drush vget cacheexclude_node_types
cacheexclude_node_types:
  article: 0
  page: 0

Configure as appropriate for your website.

Web server cache headers for static assets

Configure your web server to send the correct cache headers for static assets.
If you are using Apache HTTP Server, add the following to Drupal's top-level .htaccess file.  

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault A0

  <FilesMatch "\.(txt|xml|js)$">
    ExpiresDefault A31536000
  </FilesMatch>

  <FilesMatch "\.(css)$">
    ExpiresDefault A31536000
  </FilesMatch>

  <FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac|eot|ttf|otf|woff|woff2|svg)$">
    ExpiresDefault A31536000
  </FilesMatch>

  <FilesMatch "\.(jpg|jpeg|png|gif|swf|webp)$">
    ExpiresDefault A31536000
  </FilesMatch>
</IfModule>

<IfModule mod_headers.c>
  <FilesMatch "\.(txt|xml|js)$\">
   Header set Cache-Control "max-age=31536000"
  </FilesMatch>

  <FilesMatch "\.(css)$">
   Header set Cache-Control "max-age=31536000"
  </FilesMatch>

  <FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|mp4|m4v|ogg|webm|aac|eot|ttf|otf|woff|woff2|svg)$">
   Header set Cache-Control "max-age=31536000"
  </FilesMatch>

  <FilesMatch "\.(jpg|jpeg|png|gif|swf|webp)$">
   Header set Cache-Control "max-age=31536000"
  </FilesMatch>
</IfModule>

Web server cache headers for PHP scripts

Consider adding the following to your .htaccess file as well to disable caching of PHP scripts:

	  <FilesMatch \.php$>
	    # Do not allow PHP scripts to be cached unless they explicitly send cache
	    # headers themselves. Otherwise all scripts would have to overwrite the
	    # headers set by mod_expires if they want another caching behavior. This may
	    # fail if an error occurs early in the bootstrap process, and it may cause
	    # problems if a non-Drupal PHP file is installed in a subdirectory.
	    ExpiresActive Off
	  </FilesMatch>

Reference documentation:

For assistance or questions, contact [email protected].

Tags: 
Last Updated: 
Wednesday, April 3, 2024