If an image is missing i return this headers : header("HTTP/1.1 404 Not Found"); header('Expires: -1'); header('Pragma: no-cache'); header('Cache-Control: no-cache');
In documentation Cloudflare says it shoud not cache this but it does. Anybody has any idee if i am doing something wrong ?
I have a rule that sets EDGE Cache TTL to 14 days. Also if this rule is active the 302 are not cached anymore.
Note that if you force an Edge Cache TTL to 14 days and use âCache Everythingâ, then for HTTP 200 & 301 responses codes weâll force that Edge Cache TTL.
For other response codes such as 302, 303, 403, 404 weâll cache those for 5 minutes. Thatâs what I think your problem is here.
What you need to do here is change your page rule to âRespect All Existing Headersâ for Edge Cache TTL and then have your origin send 14 day TTLs when you want the response to be cached (e.g. HTTP 200s), and âno-cacheâ when you donât want caching (e.g. HTTP 404s).
So, after discussing with support and some testing i reached the following conclusion.
If i create a page rule with only Edge Cache TTL active then, all 404âs will be cached for an unknown period of time, all 302 will not be cached at all. For the 302 to be cached i will have to activate the âcache everythingâ option.
I donât know if you plan to change this but how it should work, in my opinion , should be like this :
If i set Edge Cache TTL then all cacheable urls that donât have the âno-cacheâ header should be cached , and of course, ignoring the TTL from headers.
If i also set the âCache Everythingâ then all urls should be cached ignoring the headers.
@ciprian thanks for the feedback on that. For the record 302 and 303 responses would be cached for 5 minutes with âCache Everythingâ and an âEdge Cache TTLâ hard coded on the page rule.
Right now for the finest grained control of caching, you should set the Edge Cache TTL to âRespect All Existing Headersâ and you can control what is cached and for how long using the origin cache headers.
@simon i donât think that Edge Cache TTL has an option âRespect ALL Existing Headersâ , i found that option only at Browser Cache Expiration but not in the page rule setting, just on the global setting.
As @ciprian pointed out, this option indeed seems not to exist. The headers returned by the origin server seem to be ignored and Cloudflare uses the TTL of the âglobal settingâ defaulting to 4 hoursâŚ
My mistake - the Edge Cache TTL setting of âRespect All Existing Headersâ is only available on the Enterprise plan.
What you should be able to do in this case is simply removed the Edge Cache TTL setting entirely from the relevant page rule - we will then obey the origin cache headers.
If the 404 response does not include caching headers what will Cloudflare do?
We are using CloudFront and origin server (since only CloudFront supports multiple origins by path)
My goal is to avoid Cloudflare from caching 404 static assets during rolling update deployment. I am NOT using Cache Everything and have Cache-Control set to no-cache in my nginx if 404 happen on my static assets. But still Cloudflare does not respect my Cache-Control header. I understand be default Cloudflare will cache 404 for 5 mins. But it will also respect Cache-Control header by reading the following article.
Replace the domain / url with one that will trigger the behaviour you have configured for 404s. Replace the 1.2.3.4 IP address with your origin server IP address or hostname.