How to cache on Cloudflare

Mon Sep 25, 2023 | 582 Words | 3 Minute

Image of cloudflare logo and some connections
© SergejDK

Introduction

Caching is a process of storing copies of data in a temporary location so that future requests for the same data can be served more quickly. Cloudflare offers a variety of caching options that can help you improve the performance and reliability of your website or application.

Why cache on Cloudflare

There are several benefits to caching on Cloudflare:

  • Improved performance: Cloudflare’s edge servers are located all over the world, so cached content can be delivered to visitors much faster than if it were served from your origin server.

  • Reduced bandwidth usage: By serving cached content from Cloudflare’s edge servers, you can reduce the load on your origin server and save on bandwidth costs. Improved reliability: Cloudflare’s edge servers are highly reliable and can continue to serve cached content even if your origin server is unavailable.

Setup

Follow these very easy steps to setup cloudflare for your website.

  1. Go to the website: cloudflare
  2. Create a new account or log into your existent one
  3. Click on add new site
  4. Input your domainname e.g. test.com
  5. Click on Continue
  6. There will be a setup guide which you should do exactly as described. It can happen that only your provider has the permissions to change the nameserver. In this case you have to contact the support of your provider and send them the setup guide.
  7. After the change you have to wait for some time. Maybe 1-2 days in worst case. After that you can go on an setup your caching behavior which follows in the next sections.

How to cache on Cloudflare

There are two ways to cache content on Cloudflare:

  • Default cache behavior: Cloudflare will automatically cache most static content, such as HTML, CSS, and JavaScript files. You can customize the default cache behavior by setting the Cache-Control header in your origin server’s responses.

  • Page rules: Page rules allow you to create custom caching rules for specific URLs or paths. For example, you could create a page rule to cache dynamic content for a certain period of time or to bypass the cache for certain requests.

Create a page rule

  1. Log in to the Cloudflare dashboard.
  2. Click the Rules tab.
  3. Click the Create Page Rule button.
  4. Enter a name and description for your page rule.
  5. In the URL pattern field, enter the URL or path that you want to apply the rule to.
  6. In the Actions section, select the Caching tab.
  7. Select the desired caching options.
  8. Click the Save and Deploy button.

Best practices

Here are some best practices for caching on Cloudflare:

  • Only cache static content: Dynamic content, such as content that is generated by a database, should not be cached.
  • Use Cache-Control headers: Cache-Control headers allow you to specify how long Cloudflare should cache content for and whether or not the content can be cached by browsers.
  • Use page rules to customize cache behavior: Page rules allow you to create custom caching rules for specific URLs or paths.
  • Purge the cache when content changes: If you update your content, you should purge the cache so that visitors see the latest version of your content.

Conclusion

Caching is a powerful way to improve the performance and reliability of your website or application. Cloudflare offers a variety of caching options that can help you get the most out of caching. As caching will help you with performance and reliability of your website this is something you should consider to make us of.

Feel free to contact me directly if you need any help.

comments powered by Disqus