After recently moving my site to Jekyll / Octopress, I was looking for a way to programmatically expire the cache for my index.html page. I mean, what good is it to update your site, if nobody can see the new content? In nginx, I added the following to my server block:
The above will set an expire date for 30 days, and set the Cache-Control header to public, and max-age=2592000
The only problem is, the index.html page would also be cached for this duration. Thankfully, Cloudflare has a pretty solid API where I found information on invalidating a specific page. So, I added new
purge tasks to the Octopress Rakefile that looked like this:
Now when you
rake deploy it’ll do the usual deploy, but then also invalidate the index.html file at Cloudflare.
rake purge will invalidate ALL assets that Cloudflare has cached for your site, and must be called specifically. You probably don’t want to use this feature that often.
And then I added the following to the end of the :deploy task:
Since I store my Rakefile along with my site content in a private git repository using bitbucket, I didn’t want to risk having potentially sensitive information directly in the Rakefile because I may eventually make the repo public. So instead, I added them as environment variables in my
~/.profile, and just have the Rakefile pull that information from my shell environment.