Enable cache in an R2 bucket
By default, R2 buckets are private. To serve R2 objects through Cloudflare's cache, you need to attach a domain name you control — called a Custom Domain — to the bucket. This creates a public URL backed by Cloudflare's network, so requests for your stored files go through Cloudflare's cache instead of hitting R2 directly every time.
Follow these steps to set up a Custom Domain for your bucket:
- Go to R2 and select your bucket.
- On the bucket page, select Settings.
- Under Public access > Custom Domains, select Connect Domain.
- Enter the domain name you want to connect to and select Continue.
- Review the new DNS record that will be added and select Connect Domain.
Cloudflare automatically adds a CNAME record that maps your domain to the bucket, generating a publicly available URL in the format [name].domain.com.
By default, Cloudflare caches R2 content at edge data centers (the data centers closest to visitors) based on cache rules. Each edge data center that gets a cache miss fetches content directly from R2.
Tiered Cache changes this by organizing data centers into a hierarchy. When a nearby data center has a cache miss, it first checks a designated upper-tier data center before going to R2. This reduces the number of requests that reach R2.
To enable Tiered Cache for R2, configure Smart Tiered Cache, which automatically selects the upper-tier data center with the lowest latency to your R2 bucket.
- Access controls: When you connect a Custom Domain, your R2 bucket becomes publicly accessible. Apply access controls to restrict who can request your files. Refer to Control cache access with WAF and Snippets for more information.
- Cacheable size limits: Files that exceed the cacheable size limits are not cached. Free, Pro, and Business plans have a limit of 512 MB per file. Enterprise plans default to 5 GB per file.
- Default cached file types: Cloudflare does not cache all file types by default. For example, HTML and JSON are not cached unless you create a Cache Rule with the appropriate settings.