Mod_Pagespeed is installed on some of our servers.

By default, PageSpeed caches publicly cacheable resources to provide maximum benefit, though the origin TTL specified in the server configuration file dictates how quickly changes made to the source can propagate to users' browser caches.

When resources on your site are cached on Cloudflare data centers, it allows them to send those resources to your visitors from a location closest to them.

This will increase your domain's loading speed and save your origin server bandwidth.

While CDN definitely helps, it is not a silver bullet that fixes all the sin in a badly made website (or CMS).

In fact, it is safe to say that when it comes to loading time, the best thing to do to get a better score is to host as close as possible to the target audience and not use any CDN at all.

So what do you do when you are hosting your website with Web Hosting Magic with Mod_Pagespeed enabled servers and using Cloudflare as another way to speed up your website?

It will be an over-kill.

If no settings are changed and no headers that prevent caching are sent from your origin server, then Cloudflare caches all static content with the extensions that Cloudflare will cache by default. This includes images, CSS, and JavaScript. For instructions on how to see whether Cloudflare is caching a resource, check here.

Here is Cloudflare's default caching behavior. Not all default behaviors are strictly RFC-compliant.

There are a few ways to alter the default behavior.

The first is through Page Rules - a feature Cloudflare offers to fine-tune how we interact with your site.

There are two specific rules that take precedence:

  1. If a Page Rule is set to "Bypass Cache", then the resources that match that page rule will not be cached. Note that we will still act as a proxy, and our other performance features will still be active - content just won't be served from our cache and fetched from the origin directly.
  2. If a Page Rule is set to "Cache Everything", then resources that match the page rule will be cached. Note that this is the only way to tell us to cache resources beyond what we consider static, including HTML.
  3. If no Page Rule is set, then Cloudflare will use the implicit Standard caching mode and look at the extension of the resource to only cache static resources.

The second way to alter what Cloudflare will cache is through caching headers sent from the origin.

Lower these to barest 5 -10 minutes and Mod_Pagespeed and Cloudflare will respect these settings, but you can also override them by specifying an Edge Cache TTL.

Here are the caching headers we consider:

  • If the Cache-Control header is set to "private", "no-store", "no-cache", or "max-age=0", or if there is a cookie in the response, then Cloudflare will not cache the resource.
  • Otherwise, if the Cache-Control header is set to "public" and the "max-age" is greater than 0, or if the Expires headers are set any time in the future, we will cache the resource.
    Note: As per RFC rules, "Cache-Control: max-age" trumps "Expires" headers. If we see both and they do not agree, max-age wins.

The third way to control Cloudflare caching behavior and browser caching behavior all in one go is by using the s-maxage Cache-Control header.

Normally we respect the max-age directive:

  • Cache-Control: max-age=1000

But if a customer would like to specify a cache timeout in the CDN which is different from the browser we can also use s-maxage:

  • Cache-Control: s-maxage=200, max-age=60

This will tell Cloudflare to cache the object for 200 seconds and the browser to cache the object for 60 seconds.

Basically, s-maxage is intended to be followed ONLY by reverse proxies (so the browser should ignore it) whilst on the other hand, Cloudflare gives priority to s-maxage if present.

Note that we will respect whichever value is higher: the browser cache setting in Cloudflare or the max-age header.

To sum up, here are the main areas to consider:

  • Check your origin's caching headers to make sure there are no overriding headers for cacheable resources (Cache-Control and Expires).
  • Cloudflare will cache static content by default. Cloudflare will default to the following TTL depending on the return code:
200 301    120m;
302 303    20m;
403        5m;
404        5m;
any        0s;
   
  • To cache more, create a Page Rule set to cache everything on the desired URL (if your web server returns a 404 when requesting this URL, Cloudflare will still cache this result for 5min only, though).
  • To avoid caching on a URL (recommended on our servers using Mod_Pagespeed), create a rule to bypass the cache.
Was this answer helpful? 2 Users Found This Useful (2 Votes)