Setting Up Caddy HTTP Server

Why Caddy

So I hadn't even heard of Caddy until recently. Looking into it I was intrigued though.

Caddy 2 is a powerful, enterprise-ready, open source web server with automatic HTTPS written in Go

Poking around at the documentation one of the things that interested me was the simple configuration of the server. I have been using nginx this year and it works well but it seems like there is a lot to learn and a lot to misconfigure. Caddy seems easy to use and ready to roll and let's face it automated SSL sounds awesome!

Install

As usual there are options for install, build from source, add package repo, or use Docker. Initially I tried Docker I was running into some issues with this method so I abandoned it pretty quickly and added the repo to my Ubuntu VPS. So I after adding the repo it was a simple install and the test page was up and running.

I'm not sure there is a ton of benefit to running this service as a container anyway. It's a stand alone http server, there is nothing extra to install, no dependencies or exotic configuration, the config is a single file. I suppose I had to add the repo and install it but I'm no Docker expert anyway so writing a Dockerfile or compose script would be more costly at this point. I just want to get this server running.

I don't think the issues had anything to do with running the app as a container but containers always add a layer of abstraction and that makes it more difficult to troubleshoot, especially with an unfamiliar program. I think it would be easy enough to switch it to a container in the future if the need arises and I will be more familiar with Caddy then.

Speaking Of Abstraction

I am using Cloudflare for DNS, proxying, cacheing, and other "services". I wanted to try Cloudflare out because I recently spoke to a production shop that seems to rely on it heavily so I wanted to check it out. I have decided to write a post about my thoughts on Cloudflare so far so I won't cover much here. I did have an issue with the Cloudflare encryption mode, by default it's set to "flexable" which tries to connect to the host via http. Caddy redirects to https by default, this caused a redirect loop. Once I figured out the abstraction between my host and client was causing the issue I was able to switch the encryption mode to "Full(strict)" so that Cloudflare would connect to https everything began to work as expected.

Configuration

With Caddy up and serving the test page all that was left was to configure it for my site. There are a few way's to configure Caddy but the Caddyfile is designed for humans.

The Caddyfile is a convenient Caddy configuration format for humans. It is most people's favorite way to use Caddy because it is easy to write, easy to understand, and expressive enough for most use cases.

This is all it takes to get a simple file server running.

example.com {
root * /var/www/html
file_server
}

Because of Cloudflare being between my server and Let's Encrypt/zeroSSL I did have to make a slightly more complicated config, but still very simple.

example.com, www.example.com {
redir https://www.example.com{uri} permanent
}

https://example.com, https://www.example.com {
root * /var/www/html
file_server
}

I'm sure that I will learn soon this is more complicated then it needs to be or there is a better way to do it but it's working for now and I understand it.