Caddy v2 file for Kirby 2 & Kirby 3 on local sites

Although I’m using this for local sites using Caddy’'s local_certs and tls internal for https, it could be adapted for a live server with other certificates.

This Caddyfile has been tested on my Mac, with PHP and Caddy installed via brew with Kirby v2 installed via the old Kirby CLI and Kirby v3 via Composer.

  • MacOS Catalina 10.15.4
  • PHP 7.4.5
  • Caddy v2.0.0
  • Kirby K2 latest and Kirby 3.3.5

It is based on snippets to allow some flexibility, Modern CMS’s like Kirby v3 don’t really need too much, but Kirby v2 proved to be a bit more tricksy.

Please let me know if you see anything amiss and I will try and correct it.

My thanks to francislavoie & Whitestrake for all their help!

# Caddy v2 file for Kirby 2 & Kirby 3 local sites
# v0.1.0
# 2020/05/10
#=====================
# Global Block
{
  email youremail@domain.tld
  local_certs
}

# Snippets
# Common server snippet use 'import common'
(common) {
  encode gzip
  php_fastcgi localhost:9000
  tls internal
  file_server
  header * {
    # You may want some other header options...
    X-Frame-Options "DENY"
    X-XSS-Protection "1; mode=block"
    X-Content-Type-Options "nosniff"
    X-Frame-Options "SAMEORIGIN"
  }
}

#---------------------
# Kirby 2 & 3 common snippet use 'import kirby'
(kirby) {  
  @blocked {
    path *.txt *.md *.mdown /site/* /kirby/*
  }
  redir @blocked /
}

#---------------------
# Kirby 2 snippet use 'import kirby2'
(kirby2) {
  # /panel files
  handle /panel* {
    try_files {path} {path}/ /panel/index.php?{uri}&{query}
  }
  
  @notPanel {
    not path /panel*
  }
  handle @notPanel {
    try_files {path}/ /index.php?&{query}
  }
}

#---------------------
# Cachebuster rewrite style.123213.css -> style.css 'import cachebuster'
(cachebuster) {
  @cachedFiles {
      not file
      path_regexp cached ~*(.+)\.(?:\d+)\.(js|css|jpg|svg|png)
  }
  rewrite @cachedFiles {http.regexp.cached.1}.{http.regexp.cached.2}
}

#=====================
# Virtual Hosts - domains need to be added to your hosts file

kirby2site.test:443 {
  root * /PATH_TO_SITE/kirby2site
  import common
  import kirby
  import kirby2
}

kirby3site.test:443 {
  root * /PATH_TO_SITE/kirby3site
  import common
  import kirby
}

kirby2cachebustersite.test:443 {
  root * /PATH_TO_SITE/kirby2cachebustersite
  import common
  import cachebuster
  import kirby
  import kirby2
}

3 Likes

Some people have contacted me privately with some questions about using Caddy v2 on a Mac/OSX… so hopefully this will be helpfull (Mac/OSX only):

  • If you use brew services to run Caddy and have it run at start up (via User Agents) currently it looks for the Caddyfile in /usr/local/etc/, so that is where to put it.

  • When you are testing your Caddyfile you should bear the above in mind and test in /usr/local/etc/ folder with caddy validate.

  • Using brew with services means it can control Caddy e.g. start/stop or restart Caddy with something like brew services restart caddy.

On a more subjective front I’ve been asked does it run well on OSX? To which the non-technical answer is yes.

Also I’ve been asked is it easier to setup than say nginx using brew? Maybe… but I do think I find the ongoing maintenance simpler with a single config file for Caddy. It is very easy to add new sites and of there course there are the very simple local certs and the very good online support from this community!

1 Like