Upgrade to Caddy v2

Caddy is an open source web server written in Go. It integrates automatic HTTPS and is extensible by adding plugins.

Last time I set up Caddy web server on my VPS, Caddy was still v0.11.0, which was almost 3 years ago:

$ caddy -version
Caddy 0.11.0 (non-commercial use only)

Now it’s 2021. My tiny lovely VPS deserves a new version of Caddy in new year!

Old Caddyfile

To show the changes in Caddyfile clearly, here’s the old version used on my VPS placed under /etc/caddy:

http://lancitou.net https://lancitou.net http://www.lancitou.net https://www.lancitou.net {
    redir https://blog.lancitou.net{uri}
}
https://blog.lancitou.net {
    gzip
    header / Cache-Control "max-age=86400"
    root /var/www/lancitou-on-hugo/public
}

And the command line used to start the server is as ugly as:

$ caddy -agree=true -conf=/etc/caddy/Caddyfile -quic -email [email protected]

Download Caddy 2

Caddy 2 can be downloaded from its download page.

I vaguely remember when I was downloading the old Caddy, I had to choose a couple of plugins to meet my needs. In Caddy 2, it always comes with all standard modules. For me, the standard Caddy 2 is pretty much enough without adding any extra packages. That’s quite a convenient improvement.

Move the freshly downloaded Caddy binary file to any searchable directory in $PATH, like /usr/local/bin, and don’t forget to add executable permission to it:

$ mv caddy_linux_amd64 /usr/local/bin/caddy
$ chmod +x /usr/local/bin/caddy

Now we can check the version of new Caddy. From caddy help, you can see multiple features are categorized into subcommands, one of which is version. That’s to say, there’s no hyphen before version:

$ caddy version
v2.3.0 h1:fnrqJLa3G5vfxcxmOH/+kJOcunPLhSBnjgIvjXV/QTA=

New Caddyfile

To upgrade the Caddyfile, you’ll need Upgrade Guide. My new Caddyfile is like this:

https://lancitou.net https://www.lancitou.net {
    redir https://blog.lancitou.net{uri}
}
https://blog.lancitou.net {
    encode gzip
    header Cache-Control "max-age=86400"
    root * public
    file_server
}

The basic structure and most of the directives are unchanged, except some minor modifications. Let’s examine it in details.

redir

The syntax for redir is the same, but compared to the old Caddyfile, all the configuration for redirecting from HTTP to HTTPS has been removed, because Caddy now supports automatic HTTP to HTTPS redirection, which is awesome!

gzip

The multiple compression formats have been included in a single directive called encode, so simply change from gzip to encode gzip.

header directive is mostly unchanged, but need to remove /.

root

root directive accepts a matcher in v2 to support changing site root depending on the request. My root path is always public under Hugo, so only need to add a * matcher token.

Besides, previously I placed Caddyfile in /etc/caddy directory, which is different from the root path, so the absolute path (/var/www/lancitou-on-hugo/public) is specified. Now I created the new Caddyfile in /var/www/lancitou-on-hugo directory, so that I could use the relative path (public).

Static files

As I’m serving static files generated by Hugo, so a file_server directive needs to be added, since Caddy 2 doesn’t assume this by default.

Start the server

Now the Caddyfile is ready, we could start the Caddy 2 web server by simply running caddy start in the same directory as Caddyfile:

$ caddy start

In the first time Caddy 2 runs, it will automatically register the certificates from Let’s Encrypt for all sub-domains.

That’s it! The whole process is actually the same as Upgrade Guide and done without any pain.

comments powered by Disqus