Could I use Cloudflare instead of Varnish Cache?

I’ve just set up a website on a new server, and I’d like to cache the text/html pages to improve speed.

But the only thing I need to work around is the fact that each page has slightly different advertising on it, and it’s based on which country the visitor is from and whether they are on a mobile or not.

I’ve looked in to using Varnish and it seems like caching different pages based on country/mobile would be possible. To set it up I was thinking I could:

  1. Use the CF-IPCountry header set by Cloudflare.
  2. Grab the User-Agent header in Varnish, and use it to create a new simple header like Mobile: true/false.
  3. Generate the page in the webserver, and add Vary: CF-IPCountry, Mobile to allow the cache to cache different pages for different mobile devices and countries.

I think that might work in Varnish, but Cloudflare has always been easy to use and reliable, so would prefer to use Cloudflare where I can.

Does Cloudflare use the Vary header when determining what to cache? If so, is the above setup of caching based on country/mobile something that I could also achieve using the Cache Everything Page Rule in Cloudflare?

Cloudflare does not support the vary header, as far as I’m aware, and support for cache per user-agent will require you to be on the enterprise plan.

You can however still accomplish this without the enterprise plan on Cloudflare’s end by using a custom Worker script. There’s a small example for this specific scenario at https://findwork.dev/blog/caching-tricks-with-cloudflare-workers/ which might be interesting to give a try. The only thing the example seems to be missing is also caching per-country, but that shouldn’t be hard to add as you can just grab the user’s country from the request object.

2 Likes

Yes CF Cache doesn’t support HTTP Vary header see Cloudflare CDN Cache To Support HTTP Vary Header

However, you can configure Cloudflare for Varnish like guest full page caching with either CF Workers or CF Business or higher paid plans.

Cloudflare does not caching dynamically generated HTML content by default (see below). So for optimal TTFB speed, you want your origin real web server to be hosted in a location closest to your majority traffic visitors and then put Cloudflare in front. For instance, my forums has 50% US visitors 40% Asian visitors and 10% Oceania. So my optimal geographic location for my origin is US West Coast as it sits in middle of US, Europe and Asian so equal round trip times for majority of visitors.

Cloudflare cache certain static content https://support.cloudflare.com/hc/en-us/articles/200172516-Which-file-extensions-does-Cloudflare-cache-for-static-content- but not dynamic/static generated html itself by default (which is what WPT TTFB is testing for).

Varnish like caching

But you can tell Cloudflare to cache dynamic/static generated html content to some extent depending on Cloudflare plan you’re on via cache everything page rule but have to be careful to only do this for static html content and not dynamic html content (otherwise you would cache private logged in user content).

Then there’s CF Enterprise plans as @arunesh90 mentioned that can cache per user agent https://support.cloudflare.com/hc/en-us/articles/229373388-Understand-Cache-by-Device-Type-Enterprise-plans-only-

example

1 Like

Thank you, wasn’t aware of Worker scripts, and that link is interesting :slight_smile:

1 Like

Thank you for such a detailed and well-written answer. This is amazingly helpful.

1 Like

You’re welcome :smiley:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.