Does CloudFlare Cache 403 and 503 By Default?


cloudflare Does CloudFlare Cache 403 and 503 By Default? cache cloud cloudflare CloudFlare

cloudflare

I have a few APIs and I want to let CloudFlare cache the results. However, I don’t want any bots to access the API so that I have a check like this:

1
2
3
4
if (isBot()) {
  return "No Bots";
}
// the rest of APIs
if (isBot()) {
  return "No Bots";
}
// the rest of APIs

If the bot visits the API URL first, the Cloudflare will cache the result and serve it to other valid users, which is not what we want to see. Luckily cloudflare has the following default caching rules:

Cloudflare will cache static content by default. We will default to the following TTL depending on the return code:

200 301    120m;
302 303    20m;
403        5m;
404        5m;
any (including 503)       0s;

To cache more, create a Page Rule set to cache everything on the desired URL (if your webserver returns a 404 when requesting this URL, we will still cache this result for 5mn only, though). To avoid caching on a URL, create a rule to bypass the cache.

Yep, in this case, the same URL should return different results to human and bots. So if you set the return code to 503 Service Temporarily Unavailable then cloudflare will ignore caching the results:

1
2
3
4
5
if (isBot()) {
  http_respond_code(503);
  die();
}
// the rest of APIs
if (isBot()) {
  http_respond_code(503);
  die();
}
// the rest of APIs

CloudFlare Technology

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
311 words
Last Post: Unit Test Methods should support float numbers comparisons with EPSILON
Next Post: How to Download Video from ted.com in Javascript?

The Permanent URL is: Does CloudFlare Cache 403 and 503 By Default?

Leave a Reply