I’ve setup a Page Rule to enabled Cache Everything / Origin Cache Control. In my application code I set a cache-control header, and CloudFlare properly caches the response.
However, in some cases an error is encountered outside the application code on my origin server, resulting in a 500/server error.
With Origin Cache Control enabled, CloudFlare will ignore any HTTP Status code, and only inspect your cache-control header it seems. So, this results in the 500 error being cached for just as long as a ‘good’ response.
Is there a way to tell CloudFlare to only use Cache-Control header when a 2xx is returned, to preventing caching when a 4xx or 5xx error is unexpectedly returned from server?
Default behavior is that a 500 is never cached. As noted in the documentation, the only 5xx range error that is cached is 501, for 1 minute.
However, when using a origin cache-control with max-age of 5 minutes, if my server returns a 500 error, the response will be cached the full 5 minutes, ignoring the status code entirely.
I understand that the default behavior w/ origin cache-control enabled would be to have cache-control header take precedence over status code. However, I was hoping there was some way to configure it to fallback to default behavior when the status code is not 2xx or 3xx for example.
As far as fixing on origin server, I’ll be making an attempt to do this. However, there are cases outside the application code where an error could occur, so it is hard to catch all possibilities there.
Workers are definitely something I’d assume could handle this, and would catch errors no matter where they come from, but wanted to avoid due to traffic levels/cost.
Unfortunate that there is not a setting for this somewhere in Cloudflare, I think it would be desired behavior for most, maybe in the future.
That’s kinda what I was leaning towards if I can figure something out there. Essentially applying at the ‘last mile’ on the server so-to-speak. I use nginx, and wondering if there’s some way to look at status code on outgoing requests, and update cache-control header when status code is 5xx.