How to use "Cache Everything" and "Cloudflare-CDN-Cache-Control" together?

Hello. I’m not sure how to configure my Cloudflare protected website to serve content faster. My application is built based on NodeJS and it’s http server can only handle around 600 req/s which is great for usual traffic but not suitable to handle holiday spikes and DDoS. So I had an idea of adding Cloudflare-CDN-Cache-Control, set s-maxage to hold a tiny value and additionally cover it with stale-while-revalidate x5 of original s-maxage value, i.e. s-maxage=2, stale-while-revalidate=10. This way during high traffic periods Cloudflare would be serving my html pages and offload the origin http server. And in case of DDoS attack page would be cached for 2 seconds on Cloudflare, and then only single request will be sent by Cloudflare to origin http server to revalidate html content. So I added the Cloudflare-CDN-Cache-Control header to all of my html pages but that didn’t work. So I’ve looked over docs and community answers and turns out I need a page rule “Cache Everything” for that so I have a few questions:

  • Does enabling “Cache Everything” rule will cache everything that is being requested from my http server immediately? And for how long if so?
  • Do I still need Cloudflare-CDN-Cache-Control or “Cache Everything” will not work without this header?
  • If the answer for the question above is yes then what if I need to avoid caching /api/* responses? Do I need to create another page rule or setting Cloudflare-CDN-Cache-Control: s-maxage=0, no-cache, no-store, must-revalidate would be sufficient?

Yes.

Use the Edge Cache TTL option in the same Page Rule to define this. Note that this is ‘best effort’ and simply means ‘we won’t keep it for longer’, not ‘we will keep it for this long’. Assets can be evicted at any time.

Cache Everything (and Edge Cache TTL) takes precedence.

Yes, putting it above the Cache Everything rule.

Ref:

https://developers.cloudflare.com/cache/about/default-cache-behavior/
https://developers.cloudflare.com/cache/best-practices/customize-cache/
https://developers.cloudflare.com/cache/about/cdn-cache-control/

So basically Cloudflare-CDN-Cache-Control header works only for assets and can’t be used to control html caching in any way?

Might work? You’d have to test it. :person_shrugging:

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.

Seems like it does acknowledge Cache-Control headers but my previous point about Edge Cache TTL taking precedence still applies.

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