Cloudflare Cache Rules give you granular control over how the CDN caches your content. Unlike the older Page Rules system, Cache Rules provide more flexibility, better matching logic, and don't count against your Page Rules quota. This guide walks you through setting up Cache Rules from scratch, whether you need to cache dynamic content, bypass caching for admin areas, or set custom TTLs for specific file types.
What Are Cache Rules?
Cache Rules are Cloudflare's modern caching configuration system. They let you define conditions (URL patterns, file extensions, query strings, headers) and specify caching behavior when those conditions match. Each rule can control cache eligibility, edge TTL, browser TTL, cache key composition, and more.
Cache Rules replaced the caching functionality of Page Rules and offer several advantages: more matching criteria, better performance, dedicated rule quota separate from Page Rules, and the ability to handle complex logic without nested wildcards.
Prerequisites
Before you begin, ensure you have:
- An active Cloudflare account with a domain added
- DNS records proxied through Cloudflare (orange cloud icon)
- Appropriate plan access (some Cache Rule features require paid plans)
- Access to the Cloudflare dashboard with sufficient permissions
Step 1: Access the Cache Rules Interface
Log into your Cloudflare dashboard and navigate to the domain you want to configure:
- Select your domain from the account homepage
- Click Caching in the left sidebar
- Select Cache Rules from the submenu
You'll see a list of existing rules (if any) and a Create rule button.
Step 2: Create Your First Cache Rule
Click Create rule to open the rule builder. You'll need to define three things: a rule name, matching conditions, and cache settings.
Name Your Rule
Give your rule a descriptive name that explains its purpose. Use names like "Cache Static Assets" or "Bypass Admin Area" rather than generic labels. Good naming becomes critical when you're managing multiple rules.
Define Matching Conditions
The When incoming requests match section controls when the rule applies. You can use:
- URI Path - matches the URL path
- URI Query String - matches query parameters
- Hostname - matches the domain or subdomain
- File Extension - matches file types
- Cookie - matches cookie names or values
- Request Headers - matches HTTP headers
- HTTP Method - matches GET, POST, etc.
Use the expression builder for simple conditions or switch to Edit expression for advanced logic.
Example: Cache All Static Assets
(http.request.uri.path.extension in {"css" "js" "jpg" "jpeg" "png" "gif" "webp" "svg" "woff" "woff2" "ttf" "ico"})
Example: Bypass Caching for WordPress Admin
(http.request.uri.path starts_with "/wp-admin/") or (http.request.uri.path starts_with "/wp-login.php")
Example: Cache API Responses for 5 Minutes
(http.request.uri.path starts_with "/api/v1/public/") and (http.request.method eq "GET")
Step 3: Configure Cache Behavior
Once you've defined when the rule matches, configure what happens. The Then section offers several settings:
Cache Eligibility
Controls whether Cloudflare should attempt to cache matching requests:
- Eligible for cache - Cloudflare will cache if response headers allow it
- Bypass cache - Always fetch from origin, never cache
- Default - Use standard Cloudflare caching logic
For most static asset rules, choose "Eligible for cache." For admin areas or user-specific content, choose "Bypass cache."
Edge Cache TTL
Sets how long Cloudflare stores the content in its edge cache before revalidating with your origin. Options include:
- Specific durations (2 hours, 1 day, 1 month, etc.)
- Respect origin headers - honor Cache-Control and Expires headers
- Bypass cache - don't cache at all
For frequently updated content, use shorter TTLs like 2-6 hours. For truly static assets, use longer TTLs like 1 month or more.
Browser Cache TTL
Controls the Cache-Control header sent to visitors' browsers:
- Specific durations
- Respect origin headers - pass through your server's headers
Typically, set browser TTL equal to or shorter than edge TTL. This ensures visitors check with Cloudflare's edge (fast) before checking your origin (slower).
Origin Cache Control
Determines whether Cloudflare respects or overrides origin cache headers:
- On - Origin headers take precedence
- Off - Cache Rule TTL overrides origin headers
Turn this Off when you want complete control via Cache Rules, even if your origin sends different headers.
Step 4: Set Rule Priority
Cache Rules are evaluated in order from top to bottom. The first matching rule wins, and Cloudflare stops processing additional rules.
After creating a rule, drag it to the correct position in the rule list. Place more specific rules above general ones:
- Bypass rules for admin/login pages (most specific)
- Custom caching for API endpoints
- Static asset caching rules
- Catch-all or default rules (least specific)
Step 5: Test Your Cache Rules
After saving your rules, verify they're working correctly:
Check Response Headers
Use curl or browser developer tools to inspect the CF-Cache-Status header:
curl -I https://yourdomain.com/style.css
Look for these values:
- HIT - Served from Cloudflare cache
- MISS - Not in cache, fetched from origin
- EXPIRED - Was cached but TTL expired
- BYPASS - Caching bypassed per rule or header
- DYNAMIC - Cloudflare doesn't cache this type by default
Verify Cache Key and TTL
Check the Cache-Control header to confirm your TTL settings took effect:
curl -I https://yourdomain.com/script.js | grep -i cache
You should see your configured max-age value.
Test Different Scenarios
Hit URLs that should match different rules and verify the behavior:
- Static assets should return HIT after the second request
- Admin URLs should return BYPASS
- Dynamic pages with bypass rules should never cache
Step 6: Common Cache Rule Patterns
Here are battle-tested configurations for typical scenarios:
Cache Everything on a Subdomain
Match all requests to a static subdomain:
(http.host eq "static.yourdomain.com")
Then set: - Cache eligibility: Eligible for cache - Edge Cache TTL: 1 month - Browser Cache TTL: 1 day
Cache HTML with Short TTL
Cache HTML pages but keep them fresh:
(http.request.uri.path.extension eq "html") or (not http.request.uri.path.extension matches "\\.[a-z]+$")
Then set: - Cache eligibility: Eligible for cache - Edge Cache TTL: 2 hours - Browser Cache TTL: 10 minutes
Bypass Cache for Query Parameters
Prevent caching when query strings indicate dynamic content:
(http.request.uri.query contains "nocache") or (http.request.uri.query contains "preview")
Then set: - Cache eligibility: Bypass cache
Cache API Responses by Endpoint
Cache public API endpoints but not authenticated ones:
(http.request.uri.path starts_with "/api/public/") and (http.request.method eq "GET") and (not http.request.headers["authorization"][*] exists)
Then set: - Cache eligibility: Eligible for cache - Edge Cache TTL: 5 minutes - Browser Cache TTL: 1 minute
Step 7: Monitor and Optimize
After deploying Cache Rules, track their performance:
Use Analytics
Navigate to Caching > Analytics to see:
- Cache hit ratio over time
- Bandwidth saved by caching
- Top cached vs uncached content
- Cache status distribution
Aim for a cache hit ratio above 80% for static content sites. Lower ratios suggest opportunities for better caching.
Adjust TTLs Based on Update Frequency
Monitor how often your content actually changes:
- Content updated hourly: 30-60 minute edge TTL
- Content updated daily: 4-12 hour edge TTL
- Content rarely updated: 1 week to 1 month edge TTL
Purge Cache When Needed
When you update content, purge the cache:
- Purge Everything - Clears all cached content (use sparingly)
- Purge by URL - Clear specific files
- Purge by Tag/Host - Clear groups of content (requires Enterprise)
Navigate to Caching > Configuration and use the Purge Cache button.
Troubleshooting Common Issues
Content Not Caching
If CF-Cache-Status shows BYPASS or DYNAMIC:
- Verify your rule matching logic with the Expression Preview
- Check origin response headers -
Cache-Control: privateorSet-Cookieprevent caching - Ensure the request method is GET (POST/PUT/DELETE aren't cached)
- Confirm the response status is cacheable (200, 301, 404, etc.)
Stale Content Being Served
If updated content doesn't appear:
- Purge the specific URL from cache
- Check your Edge Cache TTL - it may be too long
- Verify origin headers aren't setting excessive cache times
- Consider implementing cache tags for granular purging
Rules Not Applying in Expected Order
If the wrong rule is matching:
- Check rule order in the dashboard - first match wins
- Review your matching logic for overlaps
- Use more specific conditions in higher-priority rules
- Test with the Rule Preview feature before deploying
Cache Rules vs Page Rules
If you're migrating from Page Rules, understand the key differences:
- Quota: Cache Rules have a separate limit from Page Rules
- Matching: Cache Rules support more operators and header matching
- Performance: Cache Rules are evaluated more efficiently
- Flexibility: Cache Rules allow multiple settings per rule
- Migration: Existing Page Rules continue to work alongside Cache Rules
You can gradually migrate caching logic from Page Rules to Cache Rules without disruption.
Conclusion
Cloudflare Cache Rules give you precise control over caching behavior without the limitations of Page Rules. Start with simple rules for static assets, test thoroughly, and gradually add more sophisticated logic for dynamic content. Monitor your cache hit ratio and adjust TTLs based on actual content update patterns. When configured correctly, Cache Rules significantly improve performance while reducing origin server load and bandwidth costs. Remember to bypass caching for sensitive areas like admin panels and user-specific content, and always test rules in a staging environment before deploying to production.
