Articles / Lightning-fast Craft CMS sites with full page caching on Laravel Forge using Blitz
Feb 05. 2019 · by Helge Sverre

Lightning-fast Craft CMS sites with full page caching on Laravel Forge using Blitz

Note: This guide was written for v1 of Blitz, Blitz v2 is now out, but all of the things in this article still applies and is compatible.

I'm going to blow your mind, with this guide.

First we need to install the Blitz plugin

Installing Blitz
BASH
# Require with composer
composer require putyourlightson/craft-blitz

# Enable the plugin
php craft install/plugin blitz

Then we need to change our configuration files, you can change all of these in the Control Panel, but I prefer to have things like this in a dedicated configuration file, to use different settings in different environments.

config/blitz.php
PHP
<?php

return [
    '*' => [
        // Whether static file caching should be enabled.
        'cachingEnabled' => true,
    ],
    'dev' => [
        // No need for caching in development
        'cachingEnabled' => false,
    ],
    'production' => [
        // Whether the cache should automatically be warmed after clearing.
        'warmCacheAutomatically' => true,

        // Whether URLs with query strings should cached and how.
        // 0: Do not cache URLs with query strings
        // 1: Cache URLs with query strings as unique pages
        // 2: Cache URLs with query strings as the same page
        'queryStringCaching' => 1,

        // The relative path to your cache folder from your public webroot.
        'cacheFolderPath' => 'cache/blitz',

        // The max number of multiple concurrent requests to use when warming the cache. The higher the number, the faster the cache will be warmed and the more server processing will be required. A number between 5 and 20 is recommended.
        'concurrency' => 5,

        // The URI patterns to include in static file caching. The second variable represents a site ID, or a blank string for all sites.
        'includedUriPatterns' => [
            // TODO: Cache everything, change this if not appropriate
            [".*"]
        ],

        // The URI patterns to exclude from static file caching (overrides any matching patterns to include). The second variable represents a site ID, or a blank string for all sites.
        'excludedUriPatterns' => [
            // TODO: Fill out, example, contact forms thank you pages or dynamic sites that show user data
            // Example: ["/contact/thanks"], ["/profile"]
        ],

        // Whether an `X-Powered-By: Craft CMS, Blitz` header should be sent.
        'sendPoweredByHeader' => false,

        // Whether the cache should automatically be warmed after clearing globals.
        'warmCacheAutomaticallyForGlobals' => true,
    ]
];

Then we need to change our general.php config file to disable the craft cache tag, as it does not play nice with the Blitz plugin cache.

config/general.php
PHP
<?php 


return [
    // More config up here that is not important...

    // Production environment settings
    'production' => [
        // Template caching does not play nice with Blitz caching, disable it
        'enableTemplateCaching' => false,
    ],
];

Note!
CSRF tokens are also cached, so if you use {{ csrfInput() }} in your template, replace it with {{ craft.blitz.csrfInput() }}, this will add a Javascript snippet to the page that will dynamically add the correct CSRF value into the form.

NGINX Setup

We use Laravel Forge to provision and manage our servers, if you're not familiar with Nginx configuration, below is a snippet that will work on a standard Laravel Forge provisioned server.

If you use Apache or another setup for NGINX, check out the server rewrite documentation and find the configuration that is relevant to you and your hosting environment.

Laravel Forge Nginx Config
PHP
# Change this
location / {
    try_files $uri $uri/ /index.php?$query_string;
}

# To this
location / {
    try_files /cache/blitz/$http_host/$uri/$args/index.html $uri $uri/ /index.php?$query_string;
}


## OR, if you only want GET requests to be served by Blitz, do this:
set $cache_path false;
if ($request_method = GET) {
	set $cache_path /cache/blitz/$host/$uri/$args/index.html;
}

location / {
	try_files $cache_path $uri $uri/ /index.php?$query_string;
}

Results

I've successfully managed to reduce the TTFB (Time to first byte) from 600-900ms to ~25ms on a $5 droplet by using this plugin and setup, really amazing results in my opinion..

This is now a default plugin on all our sites.

Helge Sverre Hessevik Liseth

Book 1:1 help from me on CodeMentor, get expert advice and help with CraftCMS, Flutter or Laravel.

Expertise
  • Flutter
  • CraftCMS
  • PHP
  • VueJS
Book session