<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom"><id>https://vic.demuzere.be/articles/tags/cloudflare/atom.xml</id><title>Vic Demuzere - Cloudflare</title><link href="https://vic.demuzere.be/articles/tags/cloudflare/atom.xml" rel="self" type="application/atom+xml" /><link href="https://vic.demuzere.be/articles/tags/cloudflare/" rel="alternate" type="text/html" /><updated>2015-01-17T23:50:00+00:00</updated><author><name>Vic Demuzere</name></author><entry><id>https://vic.demuzere.be/articles/caching-static-website-with-cloudflare/</id><title>Caching a static website on Cloudflare</title><link href="https://vic.demuzere.be/articles/caching-static-website-with-cloudflare/" rel="alternate" /><updated>2026-05-13T18:17:26+00:00</updated><published>2015-01-17T23:50:00+00:00</published><content type="html"><![CDATA[<p><a href="https://www.cloudflare.com" title="Cloudflare">Cloudflare</a> is a cloud-based solution to protect your website against attacks.
One way to mitigate (DDoS) attacks is providing a geographically distributed network of <a href="https://en.wikipedia.org/wiki/Point_of_presence" title="Point of Presence">POP</a>s with a lot of bandwidth.
And that's exactly what they did: Cloudflare maintains a <a href="https://en.wikipedia.org/wiki/Content_delivery_network" title="Content Delivery Network (CDN)">content delivery network</a> with <a href="https://www.cloudflare.com/network-map" title="Cloudflare's Network">30 data centers</a> around the world.
Even if you have no reason to suspect being attacked any time soon, the speed improvement is well worth it.</p>
<p>Setting up their service is pretty straightforward. Move your domain to their nameservers and select the subdomains that have to be protected.
Important to note: Using the CDN routes all traffic to your website through the Cloudflare network. This means that you're basically allowing them
to do a <a href="https://en.wikipedia.org/wiki/Man-in-the-middle_attack" title="Man-in-the-middle Attack">man-in-the-middle attack</a> on your visitors. This article is aimed at static websites, so it doesn't really matter, but it's something to keep in mind.</p>
<h2 id="caching">Caching<a href="#caching" aria-label="Link to heading 'Caching'" data-heading-content="Caching" class="anchor"></a></h2>
<p>By default, <a href="https://www.cloudflare.com" title="Cloudflare">Cloudflare</a> caches resources that are supposed to be static on most websites. This includes images, stylesheets and javascripts.
Your server is contacted for every pageview, but most assets are offloaded to their edge servers.
This is great for a dynamic website, as we want to make sure that visitors see their personalized version of the website.
Static websites, however, may benefit from being cached completely.</p>
<p>First things first. In the Cloudflare site settings page, we see three cache levels:</p>
<ul>
<li>
<p><strong>Aggressive</strong>: The complete URL is used to identify a resource.</p>
<p>http://example.com/image.png?including=query-string</p>
</li>
<li>
<p><strong>Simplified</strong>: The query string is ignored when identifying a resource.</p>
<p>http://example.com/image.png?ignore=this</p>
</li>
<li>
<p><strong>Basic</strong>: Only caches resources without query string.</p>
<p>http://example.com/image.png</p>
</li>
</ul>
<p>The cache level setting only applies to those resources that Cloudflare decided to cache.
I usually set this to <em>Aggressive</em> for static websites, as it allows me to change a hash in the query string to invalidate the cache when updating a page.</p>
<p>A second interesting setting is the <em>minimum expire TTL</em>, this allows you to overwrite the expire time in the <code>cache-control</code> header.
When setting up a static website, I expect these headers to be set correctly on the origin server, but this may come in handy when you have little control.</p>
<h2 id="cache-everything">Cache Everything<a href="#cache-everything" aria-label="Link to heading 'Cache Everything'" data-heading-content="Cache Everything" class="anchor"></a></h2>
<p>The setting we're looking for is hidden deeper in the Cloudflare admin panel. In order to make their CDN cache everything, you need to create a page rule.
Page rules define custom settings for a given subset of your website. Click the gear icon next to your website and select <em>Page Rules</em>. Between the rule settings an item named <em>Custom caching</em> appears. Set this to <em>Cache everything</em> to, well, cache everything. This only applies to the URL pattern specified for the page rule.</p>
<p>Two other interesting settings:</p>
<ul>
<li>
<p><strong>Edge cache expire TTL</strong>: Specifies how long a resource has to be cached on the Cloudflare edge servers. The longer they are allowed to cache it, the more
bandwidth you'll save. Decide how big this value may be depending on how much you update your static website. You may always clear their edge cache using the
administration panel. (This setting is only available when <em>Cache everything</em> is selected.)</p>
</li>
<li>
<p><strong>Browser cache expire TTL</strong>: This setting overwrites the <em>minimum expire TTL</em> from the global site settings, for this specific URL pattern.</p>
</li>
</ul>
<p>Try it out! Depending on the number of visitors, caching everything may save quite a lot of bandwidth! If you have only a few visitors per month, you may not
even see it, especially when they are geographically distributed. It looks like Cloudflare does not share resources between edge servers. If a resource
is not available in the <a href="https://en.wikipedia.org/wiki/Point_of_presence" title="Point of Presence">POP</a> closest to your visitor, it contacts the your server.</p>
<div class="footer"><hr><p>Thanks for subscribing to my feed!</p><p>Feel free to e-mail comments or corrections to <a href="mailto:vic@demuzere.be?subject=RE%3A%20Caching%20a%20static%20website%20on%20Cloudflare">vic@demuzere.be</a>.</p></div>]]></content></entry></feed>