Customizing Cloudflare's cache

Learn the common methods to customize Cloudflare’s cache settings to extend caching to additional resources.


Overview

Cloudflare automatically caches several static file extensions unless: 

  • your origin web server sends certain Cache-Control headers for your content, or 
  • Cloudflare is instructed to cache additional content via a Cache Everything Page Rule.

Cloudflare includes advanced configurations for Business and Enterprise customer plans, including Cache on Cookie and Bypass Cache On Cookie. Enterprise plans are also allowed Custom Cache Keys.


Cache additional content at Cloudflare

Caching additional content at Cloudflare requires a Cache Everything Page Rule.  Without creating a Cache Everything Page Rule, dynamic assets are never cached even if a public Cache-Control header is returned. When combined with an Edge Cache TTL > 0, Cache Everything removes cookies from the origin web server response. 

Do not use Cache Everything for admin sections of your website or pages that require a login. To prevent Cloudflare from caching specific URLs, create a Page Rule with Cache Level set to Bypass. Then, click the up/down arrows to drag that rule above the Cache Everything Page Rule in the dashboard, as shown below.

a gif of one page rule being dragged and dropped from the second position to the first in the cloudflare page rules ui

To create a Cache Everything Page Rule, follow these steps:

1. Log in to your Cloudflare account.

2. Choose the appropriate domain.

3. Click the Page Rules app.

4. In the textbox under If the URL matches, create a URL pattern to differentiate your website’s static versus dynamic content. 

5. Choose the Cache Level setting and then the Cache Everything submenu setting.  

6. Click Save and Deploy.

7. Verify your resources are cached by checking the cache response returned by Cloudflare.

Cloudflare caches XML responses when using Cache Everything.  By default, jquery's getScript explicitly bypasses cache by appending a timestamp query string unless the behavior is disabled.  For further details on how to adjust Cloudflare’s behavior in regards to query strings, refer to our article on Understanding Cloudflare Caching Level.


Example configurations to customize caching

Some possible combinations of origin web server settings and Cloudflare Page Rules include:

  • Create a directory for static content at your origin web server.  For example, create a /static/ subdirectory at your origin web server and a Cache Everything Page Rule matching the *example.com/static/* URL pattern.
  • Append a unique file extension to static pages.  For example, create a .shtml file extension for resources at your origin web server and a Cache Everything Page Rule matching the *example.com/*.shtml URL pattern.
  • Add a query string to a resource’s URL to mark the content as static.  For example, add a static=true query string for resources at your origin web server and a Cache Everything Page Rule matching the *example.com/*?*static=true* URL pattern.

Resources that match a Cache Everything Page Rule are still not cached if the origin web server sends a Cache-Control header of max-age=0, private, no-cache, or an Expires header with an already expired date. Include the Edge Cache TTL setting within the Cache Everything Page Rule to additionally override the Cache-Control headers from the origin web server.


Related resources

Not finding what you need?

95% of questions can be answered using the search tool. This is the quickest way to get a response.

Powered by Zendesk