Published on

Caddy: Getting Started

Authors

The year is 2024. If you're still struggling with Nginx and Apache configurations, you need to take a good, long look in the mirror and ask, "Is there a better way?"

Good news! There is, and it's called Caddy Server. Backed by names you know, like Stripe, Tailscale, and us, of course. From the documentation, 'Caddy is a powerful, extensible platform to serve your sites, services, and apps, written in Go.'

What's it got?

  • Web server functions, like reverse proxy and file server
  • RESTful HTTP API
  • Structured logging
  • Shared storage/clustering
  • Human-friendly configuration
  • Automatic HTTPs
  • A well-maintained and robust plugin/module ecosystem

Sounds like a lot, right? That's because Caddy can do a lot, but can also be used really simply. Let's jump in!

If you haven't yet, go ahead and download and install Caddy.

Your First Caddyfile

Once Caddy is installed, pull up your favorite text editor and open up the default configuration file:

Linux: /etc/caddy/Caddyfile

BSD: /usr/local/etc/caddy/Caddyfile

There's lots of information in the default configuration file, but we don't need any of it. You can start by deleting everything in the document, and replacing it instead with:

/etc/caddy/Caddyfile
localhost {
    respond "Caddy is online!"
}

Save the file and use the reload action to keep Caddy running while it attempts to load the new configuration:

On Linux: $>sudo systemctl reload caddy

Or BSD: $>sudo service caddy reload

You can run curl or open a web browser, navigate to the IP of the server, and you'll see your response: "Caddy is online!"

What happens if the configuration is bad?

Zero-downtime configuration changes: Caddy will keep running the old configuration if the new one contains any errors. Pretty cool, right?

Serving Files

Okay, we know the server is talking and we know how to reload our configuration file when we change it. Let's serve some assets. Say you have some shared web assets in a folder, '/usr/share/static', and you want to serve them with Caddy. It's as easy as these three lines:

/etc/caddy/Caddyfile
localhost {
    root * /usr/share/static
    file_server
}

Reload the server again and you should be able to navigate to the files in your static folder.

Add HTTPS in Seconds

We aren't going to be serving these static assets insecurely! After creating a DNS record and pointing it to our Caddy server, let's modify our existing configuration to serve our static assets from our domain at static.skip2.net:

/etc/caddy/Caddyfile
static.skip2.net {
    root * /usr/share/static
    file_server
}

After we reload our configuration, Caddy is going to automatically attempt to reach out to Let's Encrypt or ZeroSSL, generate a CSR and request a certificate, verify with HTTP, and then download and install that certificate. The whole process usually takes less than a couple of minutes. You can read more about this process in the docs.

If all has gone well, after a few minutes, we can navigate to our website and see that it's using a signed SSL certificate.

Reverse Proxy

Alright, we've got our static assets on the internet, served with a free SSL certificate from Let's Encrypt or ZeroSSL. It's time to serve our main site, a Node.js app server running on port 9000, to our users, with SSL. We'll add another entry to our configuration:

/etc/caddy/Caddyfile
static.skip2.net {
   root * /usr/share/static
   file_server
}

skip2.net,
www.skip2.net {
   reverse_proxy localhost:9000
}

After reloading our configuration again, Caddy provisions another SSL certificate for both of the new domains and begins proxying traffic to our application. You don't need to worry about setting headers or anything else, Caddy takes care of this for you.

Wrapping up

Welcome to Caddy! If you've followed along, then you've been able to use Caddy to serve static files and proxy requests to an application, all while automatically verifying and provision SSL certificates. In the next tutorial, we'll look at modules and plugins for Caddy.

Want to learn more about Caddy?

Visit their official website at caddyserver.com Read through the docs at caddyserver.com/docs If you use it, sponsor the project (we do!) caddyserver.com/sponsor

This post is one of a series of tutorials about Caddy Server. You can view all relevant posts here.

Learn about Skip2

Sign up for our newsletter

Get Started