httpInclude not seeming to be parsed

1. The problem I’m having:

I’m attempting to get {{httpInclude ...}} working, but rather than including the contents of the URL in question, it’s outputting the literal string - I’m not sure what I’m not doing correctly.

2. Error messages and/or full log output:

{{httpInclude "/_includes?action=blitz%2Finclude%2Fcached&index=1014403699" }}

is being output into the web page instead of the content of that file being output. You can see this in action here:
https://blitz-caddy.viewcreative.agency/test-page-one

And you can see that the “file to be included” has content by looking here:
https://blitz-caddy.viewcreative.agency/_includes?action=blitz%2Finclude%2Fcached&index=1014403699

3. Caddy version:

v2.7.4 h1:J8nisjdOxnYHXlorUKXY75Gr6iBfudfoGhrJ8t7/flI=

4. How I installed and ran Caddy:

Following the official instructions.

a. System environment:

Ubuntu 22.04LTS, systemd

b. Command:

n/a

c. Service/unit/compose file:

n/a

d. My complete Caddy config:

# Snippets (see: https://caddyserver.com/docs/caddyfile/concepts)
(staticFileCache) {
        @static {
                file
                path *.ico *.css *.js *.gif *.jpg *.jpeg *.webp *.png *.svg *.woff2
        }
        header @static Cache-Control max-age=5184000
}

(blitzNoQueryString) {
        @blitzCache {
                method GET
                not expression {query} != ''
        }
        route @blitzCache {
                try_files /cache/blitz/{host}{uri}/index.html {path} {path}/index.php?{query}
        }
}

(wordpressBruteForce) {
        @block1 {
                path /wp-*
        }
        abort @block1

        @block2 {
                path *wp-includes*
        }
        abort @block2
}

blitz-caddy.viewcreative.agency {
        root * /websites/blitz-caddy/web
        encode gzip zstd

        file_server
        import staticFileCache
        import blitzNoQueryString
        import wordpressBruteForce

        log {
                output file /websites/_logs/blitz-caddy.log
        }

        php_fastcgi unix//run/php/php8.1-fpm.sock
}

5. Links to relevant resources:

Blitz is a caching utility for CraftCMS. It takes server responses from PHP and writes them to disk as a static file; then we’re using Caddy to serve those static files if they exist, as long as there isn’t a query string in the URL.

Blitz also supports SSI for nGinx and Apache.

I’m trying to get it to support “Caddy SSI”, which seemed like it shouldn’t need anything more than altering the SSI syntax from

<!--#include virtual="/_includes?action=blitz%2Finclude%2Fcached&index=12345" -->

to

{{httpInclude "/_includes?action=blitz%2Finclude%2Fcached&index=12345"}}

However while I’ve managed to alter the plugin to do that, it is not working on the Caddy side of things.

I’m not seeing the templates directive in your Caddyfile config. You need to enable the templates handler for that to do anything.

Since Caddy templates don’t use HTML-style comment syntax, you’ll need to make sure that all user-generated content is filtered, to make sure other content doesn’t get parsed as templates. You should only use templates if you’re sure that all content produced by your upstream app is safe to parse as a template.

1 Like

I knew there would be something stupid I was missing, working too late! Thanks!

For the sake of completeness if anyone else stumbles onto this, and because the docs don’t have examples of things in context, here’s a working Caddyfile that also supports “SSI”:

# Snippets (see: https://caddyserver.com/docs/caddyfile/concepts)
(staticFileCache) {
        @static {
                file
                path *.ico *.css *.js *.gif *.jpg *.jpeg *.webp *.png *.svg *.woff2
        }
        header @static Cache-Control max-age=5184000
}

(blitzNoQueryString) {
        @blitzCache {
                method GET
                not expression {query} != ''
        }
        route @blitzCache {
                try_files /cache/blitz/{host}{uri}/index.html {path} {path}/index.php?{query}
        }
}

(wordpressBruteForce) {
        @block1 {
                path /wp-*
        }
        abort @block1

        @block2 {
                path *wp-includes*
        }
        abort @block2
}

blitz-caddy.viewcreative.agency {
        root * /websites/blitz-caddy/web
        encode gzip zstd

        file_server

        import staticFileCache
        import blitzNoQueryString
        import wordpressBruteForce

        templates {
                between <!--#caddy -->
        }

        log {
                output file /websites/_logs/blitz-caddy.log
        }

        php_fastcgi unix//run/php/php8.1-fpm.sock
}

The “new bit” is the

        templates {
                between <!--#caddy -->
        }

This also changes the “start of a template instruction” and “end of a template instruction” from the default {{ and }} delimiter strings.

In the context of a Craft project those defaults may be problematic because they are valid Twig start and end delimiters. In debugging scenarios it’s possible that the end rendered HTML would contain those delimiters, causing an issue here.

I have therefor changed them to delimiters that are sure to not be present in any HTML:

  • <!--#caddy to mark the opening delimiter
  • --> to mark the matching end delimiter

This means that a complete string output in an HTML file that would trigger an “SSI” might be:
<!--#caddy httpInclude "/path/to/cached-output" -->

1 Like

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