geerlingguy / jeffgeerling-com

Drupal Codebase for JeffGeerling.com
https://www.jeffgeerling.com
GNU General Public License v2.0
41 stars 2 forks source link

Set up Cloudflare purging when publishing/editing blog posts #146

Open geerlingguy opened 2 years ago

geerlingguy commented 2 years ago

Any time I publish or save an edit to a blog post, I'd like to automatically trigger a Cloudflare purge on the following paths:

  1. The blog post URL
  2. The home page (/)
  3. The blog page (/blog)

Might not always need to do the latter two, but it's safe enough to do anyways, IMO.

One thing to consider—the cache might still be stale in Nginx itself, since it's storing a 5-10 minute separate cache of everything. So one thing I could do is disable that cache since I'm routing everything through Cloudflare now. Maybe.

Related to #141.

minecraftchest1 commented 2 years ago

I have been running a WordPress blog behind Cloudflare for a few days and made a few changes with Cloudflare set to provide a 4 hour cache time in both the cache page in the Cloudflare Dashboard, as well as with a page rule and it has always updated immediately for me. From that, I don't think that caching will be a problem for you. Granted, I haven't played with it very much and I am using WordPress, not Drupal so your results may very.

TLDR: I would check to see if caching is actually going to be an issue before trying to do automatic cache clearing.

geerlingguy commented 2 years ago

I have a 1 hour TTL set in Cloudflare, and it definitely respects that—I'd rather make sure the RSS feed and home page are updated immediately, so the Drupal module integrates with Cloudflare's API to purge by path, and that should hopefully be enough for me.

The one thing I need to look into is if I can get that API functionality on a free plan. Not sure if I'd need Pro or Enterprise to do that :/

I remember it being easier to set up with Varnish or Fastly (which uses Varnish IIRC).

minecraftchest1 commented 2 years ago

I haven't had issues with using the api on the cloudflare free plan, but I haven't played with it much.

userbradley commented 1 year ago

The cloudflare free plan will be fine.

As long as you don't send more than 1200 requests per 5 minutes

I am looking at doing something similar for my site

I'll take a stab at this, and let you know how simple/ annoying it was

geerlingguy commented 1 year ago

For now I've actually set this up using an Ansible playbook, since the purge also has to happen after Nginx static cache is cleared, and I'd rather not do all that through Drupal/PHP.

See my blog post on how I set it up: Clearing Cloudflare and Nginx caches with Ansible