Configuring Cache Rules for Advanced Performance and TTL Control
Learn how to create custom Cache Rules in Cloudflare to set TTL values and bypass cache for specific paths, giving you total control over edge performance.
Previously in this course, we explored Global Network and Edge Caching, where we identified the difference between cacheable static assets and dynamic content. Now, we move from passive observation to active control by configuring Cache Rules.
While default settings work for many sites, professional applications often require granular control. You might need to cache a specific API response for five minutes or ensure that sensitive admin routes are never stored on the edge. Cache Rules are the primary tool for this level of precision.
Understanding Cache Rules and TTL
Cache Rules allow you to define exactly how Cloudflare handles requests based on URL patterns, headers, or other request properties. The most critical setting you'll manage is the Time To Live (TTL).
TTL tells Cloudflare how long to keep an asset in its edge cache before checking back with your origin server. A longer TTL improves performance by reducing origin load, but it risks serving stale data. A shorter TTL keeps content fresh but increases traffic to your origin.
Why You Need Custom Rules
Default caching behavior is often broad. By creating custom rules, you can:
- Bypass Cache: Ensure dynamic user-specific data is never cached.
- Override TTL: Force a longer cache duration for static assets that rarely change.
- Control Eligibility: Explicitly define which file types or paths should be eligible for the CDN.
Creating Your First Cache Rule
To create a rule, navigate to Caching > Cache Rules in your Cloudflare dashboard. We will build a rule to bypass the cache for a specific administrative path, /admin/*.
- Click Create rule.
- Name it "Bypass Admin Cache".
- Under When incoming requests match, define your filter:
- Field:
Hostnameequalsyourdomain.com - AND Field:
URI Pathstarts with/admin/
- Field:
- Under Cache eligibility, select Bypass cache.
- Click Deploy.
Any request matching this pattern will now skip the edge cache entirely, hitting your origin server directly every time.
Setting Custom TTL Values
For assets like CSS or JS files that have versioned filenames (e.g., main.a1b2c3.js), you can set a long TTL to maximize performance.
- Create a new rule named "Cache Static Assets".
- Filter for
File extensionequalsjsorcss. - Under Cache eligibility, choose Eligible for cache.
- Set Edge TTL to "Override origin" and set the value to 1 month (2592000 seconds).
Hands-on Exercise: Implementing an API Cache
In our ongoing project, let’s assume you have an API endpoint that returns a list of products. This data only changes once an hour.
Task:
- Create a Cache Rule for the path
/api/products. - Set the Edge TTL to 3600 seconds (1 hour).
- Ensure the Browser TTL is also set to respect the edge settings.
- Verify by checking the
cf-cache-statusheader in your browser's Network tab after the first request.
Self-check: If you see HIT after the first request, your rule is working. If you see MISS repeatedly, check your filter logic to ensure it matches the request path exactly.
Common Pitfalls
- Over-caching: Accidentally caching pages that contain sensitive user information. Always use "Bypass cache" for login, profile, or checkout pages.
- Ignoring Headers: Your origin server might be sending
Cache-Control: privateorno-storeheaders. Cloudflare respects these by default; if your rule isn't working, check your origin headers first. - Rule Order: Rules are processed in order. If you have a broad "Cache All" rule at the top, a more specific "Bypass" rule below it might never be reached. Keep your most specific rules at the top.
FAQ
Q: How do I know if my rule is working?
Use the browser's Network tab (F12) to inspect the response headers. Look for cf-cache-status: HIT or MISS. If you see BYPASS, your rule is correctly forcing the request to skip the cache.
Q: Can I use Cache Rules to purge files? No. Cache Rules define how content is cached. To remove content from the cache, you need to perform a purge, which we will cover in the next lesson.
Q: What is the difference between Edge TTL and Browser TTL? Edge TTL controls how long Cloudflare keeps the file. Browser TTL tells the visitor's browser how long to keep the file locally. Setting these correctly is key to Edge Caching with Surrogate Keys.
Recap
Cache Rules provide the surgical precision needed for modern web performance. By defining TTLs and bypasses, you protect your origin while optimizing the user experience. You now have the power to decide exactly what lives on the edge and for how long.
Up next: We will dive into Managing Cache via Dashboard and API, where you'll learn how to clear that cache when content changes.
Work with me

Next.js Full-Stack Web App Development
A fast, SEO-ready full-stack web app built with Next.js 16 — from idea to deployed product, by an engineer who ships to production.

Custom Email & File Storage System on Cloudflare (Google Workspace Alternative)
Your own private email + file storage suite on your domain — unlimited mailboxes, no per-seat fees. A self-owned Google Workspace alternative for a flat ~$5/month.