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
- CloudFlare: Change Security Level Value Programmatically for Multiple Domains via PHP/Python/Bash Script
- How to Transfer Domain From Namesilo to CloudFlare Registra?
- A Simple Rate Limiter for CloudFlare Workers (Serverless API) based on KV Stores
- Cloudflare Worker Unexpected High Usage of API Requests - How to Avoid Surprising Billing?
- Tutorial: How to Set Up a API Load Balancer by Using CloudFlare Worker?
- How to Fix CloudFlare Error 1101 (Worker threw exception)?
- Using CloudFlare Worker Serverless Technology to Deploy a Load Balancer (RPC Node) for Steem Blockchain
- Set Up Website Health Checks (Canaries) using CloudFlare
- CloudFlare Internet Summit - Recaps
- Does CloudFlare Cache 403 and 503 By Default?
- The PHP Page Rule Checker of CloudFlare
- Cloudflare Offers Dedicated SSL Certificates
- How to Clear CloudFlare Caches of Multiple URLs using PHP Script with CloudFlare API?
- Does CloudFlare (Cache Everything) Affect the Adsense?
- Posting SQL code on bbForum Triggers Security Rules by CloudFlare
- How to Offload Your Server by Using CloudFlare - Cache Everything?
–EOF (The Ultimate Computing & Technology Blog) —
loading...
Last Post: Unit Test Methods should support float numbers comparisons with EPSILON
Next Post: How to Download Video from ted.com in Javascript?