Caching basics

I must admit to be somewhat confused by what is cached by Cloudflare and what isn’t.

Say my domain is example.com, which is registered with Cloudflare.

I have two types of traffic. Firstly I have a web host (another provider) at IP address 321.54.6.987 which serves html and associated content via e.g. www.example.com/mypage.html. For this I have the following DNS entries on Cloudflare:

CNAME / www / example.com / Proxied
A / example.com / 321.54.6.987 / Proxied

And then I have a separate self-hosted server at IP address 12.345.678.99 which responds to http requests, for example at myapp.example.com/getData. For this, I have the following entry on Cloudflare:

A / myapp / 12.345.678.99 / Proxied

Am I right in thinking that, in order for content to be cached at Cloudflare servers, the following two conditions need to be met?

(a) the relevant DNS entry must be set to Proxied
(b) there must also be a Cache Rule in place for the relevant route

It’s not enough just to enable proxying? Also need to have a matching Cache Rule?

For example a call to myapp.example.com/getData will not be cached unless there is a Cache Rule which matches one or both or myapp.example.com and route /getData, or some other rule that matches the request?

What about calls to www.example.com/mypage.html? Again, there must be a matching Cache Rule otherwise nothing is cached?

It depends on the content.

By default, Cloudflare caches static content, and does not cache dynamic content. HTML is considered dynamic. Cloudflare infers the cacheability of assets by their file extension.

You can create a Cache Rule to make Cloudflare cache dynamic content, or not cache static content.

@cbrandt OK thanks, that’s helpful.

So (and this may be a dumb question but I’d just like to put it beyond doubt)… it’s also the case that caching would only happen if the relevant DNS entry is set to Proxied… otherwise Cloudflare is simply passing on traffic without doing anything with it?

And for my call to myapp.example.com/getData, this is treated as dynamic content (even if it happens to be serving e.g. a .jpg file), because it doesn’t have one of the static file extensions? And therefore not cached by default, unless I have a suitable Cache Rule in place to catch it?

And www.example.com/mypage.html itself is not cached (unless I have a Cache Rule which says otherwise)… but static resources loaded from it (e.g. www.example.com/images/flower.png) is cached by default (again unless there is a Cache Rule which says it shouldn’t be)?

1 Like

Yes, yes*, and yes.

*Assuming your requests for .jpg files (which would ordinarily be cached) do not contain the file extension, they wouldn’t be cached unless a cache rule says otherwise.

1 Like

Indeed, the request would be in the form myapp.example.com/getImage or myapp.example.com/getData, i.e. without any extension.

Related to this is whether there is any effect in setting the DNS entry to Proxied if you’re only ever serving dynamic content (via requests like the above), and don’t have any Cache Rule to alter the default behaviour (i.e. no caching)?

I’m trying to troubleshoot an issue where an end user is sometimes seeing stale (dynamic) content (i.e. older than what the Cache-Control header specifies)… the requests for this content are proxied via Cloudflare but there is no matching Cache Rule so my understanding is that there is no caching at Cloudflare (so the stale content isn’t from Cloudflare)… but is there anything else that is caused by setting the DNS entry to Proxied? Should it be the same as disabling proxying altogether, or does something else happen I’m not aware of when proxying is in place? The dynamic content is returned with an appropriate Cache-Control header, so if it’s being cached anywhere along the line (e.g. on the user’s device), it should only be for the desired amount of time and no longer. And so far as I can tell, I’ve set all Cloudflare caching to “respect headers” so even if it was being cached by Cloudflare, it should only be for the appropriate time.

All security (WAF, DDoS protection, etc.) and performance (cache, image optimization, etc) features of Cloudflare depend on your domain being set to :orange:. So it all depends on what you want to get out of Cloudflare for the given hostname.

1 Like

@cbrandt this page says that “Cloudflare does not cache the resource when: The Cache-Control header is set to private” and “Cloudflare does cache the resource when: The Cache-Control header is set to public”.

Does that apply even when a Cache Rule is in place for a particular request? For example, if I have a Rule in place for dynamic content at myapp.example.com/getData and the Cache-Control header for these responses is set to private they still won’t be cached at Cloudflare?

It depends on how the Rule is created. The Rules engine is so flexible, it allows you to set a Cache Rule to respect a Browser Cache set by the origin, while overriding the Edge Cache eligibility.

Once you hit the Create New Rule button, it first gives you the option of whether to bypass cache (meaning bypass Cloudflare cache) or make it eligible for caching. Then for the options Edge Cache and Browser Cache, you have further options to either override or respect the origin header for caching.

The best course of action is to create a staging subdomain and create Cache Rules for different scenarios, see how they work.

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.