Caching in Ghost with nginx

This site runs on Ghost, and i just updated it to the latest version. It is in turn served by nginx using a reverse proxy. Since this site is updated quite seldom a good cache strategy is needed, and this is handled by nginxs own cache function. This works great and nginx is super fast in sharing static and cached content. The only drawback is when i actually change the content i needed to clear the cache manually.

Ghost supports web hooks as a way to create custom integrations, and i decided to use a web hook to clear and warm the cache when ever the site is update. To do this i configured a web hook on the site changed trigger, that calls a script on the server to clear and warm the cache. I wrote it in php because that was the fastest way for me, but it can obviously be done in any language you prefer. The script consists of two parts:

function rmdir_recursive($dir) {
    $it = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
    $it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
    foreach($it as $file) {
        if ($file->isDir()) rmdir($file->getPathname());
        else unlink($file->getPathname());

The method is called and passing it the path to the nginx cache directory. Once that is completed the following code is run with the url to the sites sitemap.

function visit_urls($url) {
        $html = file_get_contents($url);
        $xml = new SimpleXMLElement($html);
        foreach ($xml->url as $url_list) {

$html = file_get_contents('');
$xml = new SimpleXMLElement($html);
foreach ($xml as $item) {

This should give you a fresh cache that contains all of the pages on the site.

Carl Cedergren
2021-08-25 08:48

<< Added the core shell page