Caddy logo
Caddy logo

Why you should use Caddy as your webserver

emember the times when the term “Webserver” was almost exclusively synonymous to the words “Apache” and “NGINX”? Well, it is the year 2020 now and a lot has changed in the last few years. With tools like Docker and Kubernetes rising in the web industry there is a growing need for more small, leightweight but still flexible web server and reverse proxy solutions with features like dynamic configuration and load balancing.

Enter Caddy - a modern, flexible and easy to configure webserver built for the modern web!

It’s easy to configure

Caddy makes use of configuration adapters (config adapters for short) — so it can be configured using multiple methods. There are two built-in: JSON and Caddyfile.

The Caddyfile is the config adapter for the average human, it is easy to understand and dead simple to use.
Let’s pretend that you just rented a virtual server instance somewhere and have setup the Caddy webserver using the package manager of your choice.

You stumble about the /etc/caddy directory and the curious human you are, opening the /etc/caddy/Caddyfile .
You are immediately amazed at what few lines of configuration are in there!

*:80 {
root * /usr/share/caddy
file_server
}

That’s it! That is everything needed to serve some basic files! (Compare this to the default nginx or apache configuration if you want to know why I am so enthusiastic about it!).

Scenario 1: Serving a SPA + separate API

Okay, let’s step it up to another common scenario. You crafted a cool Angular/React/Vue.js App and nice API in the programming language of your choice (Go, .NET Core, etc. ).

The API might be listening on port localhost:4321 . Here is the Caddyfile for everything:

# The domain you want to use
mycoolapp.com
# set the root directory
root * /var/www/html/spa
# Compress responses according to Accept-Encoding headers to speed
# things up where possible
encode gzip zstd
# Configure path router support for Angular or another SPA framework
try_files {path} {path}/ /index.html
# Send every request to starting with '/api/' to the backend
reverse_proxy /api/* localhost:4321
# Serve everything else like assets from the file system
file_server

Done! Restart your caddy server and access your application!

Scenario 2: Serve a PHP app using FastCGI

Many apps on the web run on PHP today. So, let’s add a PHP application for demonstration purposes on the same server alongside the SPA.

To add another site definition, we wrap everything after the domain in curly braces and then add a new site definition like this (assuming you installed the package php7.4-fpm from deb.sury.org on a standard Debian/Ubuntu machine):

mycoolapp.com {
encode gzip zstd
root * /var/www/html/spa
try_files {path} {path}/ /index.html
reverse_proxy /api/* localhost:4321
file_server
}
myphpapp.com {
encode gzip zstd
root * /var/www/html/php-app
php_fastcgi unix//run/php/php7.4-fpm.sock
file_server
}

And thats it! Your PHP application should be up and running!

Automatic HTTPS

If you setup a site using Caddy on a publicly reachable server with the domain pointed to it, you will have noticed that your site got Let’s Encrypt certificates automatically.

Caddy serves any site with a domain on HTTPS by default and provision a Let’s Encrypt certificate if it is publicly reachable on the internet.

It will also automatically keep renewing those certificates for you! If you want to learn more about Caddys automatic HTTPS feature, head over to the Documentation.

Configuration API

At the start of this article, I wrote about the different config adapters supported by Caddy. Another one of those is the JSON file adapter. But it not only comes with a file adapter, it also has a JSON configuration API.

Admittedly, the JSON route is way more verbose, but here is an example straight from the official documentation:

curl localhost:2019/load \
-X POST \
-H "Content-Type: application/json" \
-d @- << EOF
{
"apps": {
"http": {
"servers": {
"hello": {
"listen": [":2015"],
"routes": [{
"handle": [{
"handler": "static_response",
"body": "Hello, world!"
}]
}]
}
}
}
}
}
EOF

You can find an introduction for the Caddy API in the documentation.

Wrapping up

So if you are tired of complicated configuration files and profit from automatic HTTPS-Certificates via Let’s Encrypt, you should definitely check out Caddy.

Setup a test environment and take it for a test drive. There are even more features available like load-balancing or Docker support.

Thanks for reading!

Software Developer, avid reader, curious human

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store