Using Origin Cache-Control, but still inspect HTTP Status Code

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?

Such a code should be only briefly cached

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.

I assume this could only be achieved with a Worker, though you could fix that on the server-side and only send the caching header in case of a success message.

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.

Yes, using workers. In general it is difficult for a caching layer to know what is (or is not) an expected response.

If you can configure your origin to return a different cache control headers for those types of responses you can obviously do it without workers and keep your settings as is.

Out of the box Cloudflare wont offer anything. It will be down to changing your server (it doesnt necessarily have to be inside the application, try reconfiguring the webserver) or to a Worker.

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.

I am not overly familiar with Nginx but I wouldnt be surprised if they support that with their script-like syntax. Though thats better a question for an Nginx forum :slight_smile:

This topic was automatically closed after 30 days. New replies are no longer allowed.