Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cache-Control Headers not respected on CloudFlare

I am trying to get some html pages to be cached, the same way images are automatically cached via CloudFlare but I can't get CloudFlare to actually hits its cache for html.

According to the documentation (Ref: https://support.cloudflare.com/hc/en-us/articles/202775670-How-Do-I-Tell-CloudFlare-What-to-Cache-), it's possible to cache anything with a Cache-Control set to public with a max-age greater than 0.

I've tried various combinations of headers on my origin Nginx server without success. From a simple Cache-Control: public, max-age=31536000 to more complex headers including s-maxage=31536000, Pragma: public, ETag: "569ff137-6", Expires: Thu, 31 Dec 2037 23:55:55 GMT without any results.

Any ideas to force CloudFlare to serve the html pages from their cache?

PS: I am getting the CF-Cache-Status: HIT on the images and it works fine but on the html pages nothing, not even CF-Cache-Status: something. With a CloudFlare page rule for html pages, it seems to work fine but I want to avoid using one, mainly because it's too CloudFlare specific. I am not serving cookies or anything dynamic from these pages.

like image 737
Hartator Avatar asked Jan 20 '16 21:01

Hartator


People also ask

Does Cloudflare respect cache headers?

Cloudflare respects the origin web server's cache headers in the following order unless an Edge Cache TTL page rule overrides the headers. Cloudflare does not cache the resource when: The Cache-Control header is set to private , no-store , no-cache , or max-age=0 .

How do I bypass Cloudflare cache?

To prevent Cloudflare from caching specific URLs, create a page rule with Cache Level set to Bypass. Next, drag that rule above the Cache Everything Page Rule in the dashboard so that the Bypass rule is above the Cache Everything.

What should add to a Cache-Control header?

private. The private response directive indicates that the response can be stored only in a private cache (e.g. local caches in browsers). You should add the private directive for user-personalized content, especially for responses received after login and for sessions managed via cookies.


2 Answers

It is now possible to get Cloudflare to respect your web servers headers instead of overriding them with the minimum described in the Browser Cache TTL setting.

Firstly navigate to the Caching tab in the Cloudflare dashboard:

Cloudflare Dashboard Caching tab

From here you can scroll down to the "Browser Cache Expiration" setting, from here you can select the "Respect Existing Headers" option in the dropdown:

Respect Existing Headers Cloudflare

Further reading:

  • Does CloudFlare honor my Expires and Cache-Control headers for static content?
  • Caching Anonymous Page Views
  • How do I cache static HTML?

Note: If this setting isn't chosen, Cloudflare will apply a default 4 hour minimum to Cache-Control headers. Once this setting is set, Cloudflare will not touch your Cache-Control headers (even if they're low or not at all set).

like image 72
mjsa Avatar answered Sep 26 '22 16:09

mjsa


I stumbled on this too. From the page it says

Pro Tip: Sending cache directives from your origin for resources with extensions we don't cache by default will make absolutely no difference. To specify the caching duration at your origin for the extensions we don't cache by default, you'd have to create a Page Rule to "Cache Everything".

So it appears that you do have to set a page rule to use this for files that CloudFlare doesn't cache by default. This page describes this in more detail,

https://blog.cloudflare.com/edge-cache-expire-ttl-easiest-way-to-override/

That said it still didn't work for me and appears not to be supported. After contacting their support they confirmed this. Respect Origin Header has been removed from all plan types. So if you have no page rules they will respect the origin header.

This doesn't help for hitting their edge cache for html pages however. To that you have set up a page rule. Once that is done you can, I believe, set your max-age as low as your plan allows. Any lower and it gets over-written. That is to say, with no page rule you could say Cache-Control: max-age:30 and it would pass through. With a page rule that include edge caching your max-age then becomes subject to the minimum time your plan allows even if the page rule doesn't specify browser cache.

like image 29
Jim Lowrey Avatar answered Sep 24 '22 16:09

Jim Lowrey