Caddy Web Server: The Swiss Army Knife of Web Hosting

Kvs Vishnu Kumar
5 min readMar 19, 2024

Caddy offers a powerful solution for deploying and managing websites seamlessly. This article explores Caddy’s automatic HTTPS, reverse proxy, virtual hosting, and extensible capabilities.

Caddy — https://caddyserver.com/

Introduction to a Web Server

A web server is a software application that serves content to clients over the internet or a local network. Its primary function is to store, process, and deliver web pages and other web content to users who request it.

The primary functions of a web server include:

  1. Listening for incoming requests: The web server listens for incoming HTTP or HTTPS requests from clients (web browsers) on a specific port (usually port 80 for HTTP and 443 for HTTPS).
  2. Processing requests: When a request is received, the web server processes the request by determining the requested resource (e.g., HTML file, image, etc.) and retrieving it from the server’s file system or generating it dynamically.
  3. Serving content: The web server sends the requested content back to the client (web browser) in the form of an HTTP response.
  4. Logging: Web servers typically log information about incoming requests, such as the client’s IP address, requested resource, date and time, and other relevant details.

A traditional web server sits in front of your application. The below example shows the typical flow of a web application.

We utilize a web server in front of our web app because it manages request handling, load balancing, reverse proxying, and numerous other tasks.

Some famous web servers that we know of are apache(httpd) and nginx.

In this article, we’ll explore Caddy which is a high-performance web server written in Go and is known for its exceptional performance and ease of use.

Caddy Web Server

Caddy is an open-source, production-ready web server written in Go. It is designed to be easy to use, secure, and fast.

Some features of caddy are:-

Automatic HTTPS

Caddy can automatically obtain and renew SSL/TLS certificates for your domain from Let’s Encrypt, making it easy to enable HTTPS on websites.

Every website needs certificates to make it HTTPS. Let’s Encrypt is a free non-profit Certificate Authority that provides TLS certificates for websites. As of this year, it is providing certificates for 363 million websites.

Let’s Encrypt certificates have a default validity period of 90 days, and they need to be renewed 30 days before expiry. Caddy handles the renewal process automatically, ensuring your website remains secure without any manual intervention on your part.

PS: Caddy also supports HTTP/2 and HTTP/3

Reverse Proxy

Caddy can also act as a reverse proxy, making it easy to handle multiple websites or applications on a single server.

For example, if you have two websites running locally on a server, each on different ports, you can use Caddy to reverse proxy both of them.

Below is a Caddyfile, which is used to define how Caddy handles incoming requests.

candletower.com {
reverse_proxy localhost:8080
}

vishnukvs.xyz {
reverse_proxy localhost:8081
}
  • For requests to candletower.com, Caddy forwards them to the backend server running on localhost:8080.
  • For requests to vishnukvs.xyz, Caddy forwards them to the backend server running on localhost:8081.

This way, we can run multiple applications on a single server and use Caddy on the front to act as a reverse proxy.

PS: candletower.com is a project of mine which provides technical analysis for US stocks. It’s completely free. Please check it out.

And vishnukvs.xyz is my portfolio.

File Server

You can also use Caddy as a file server. With this functionality, you can easily serve static content, such as HTML, CSS, JavaScript, images, and other files.

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

In this case, we are serving the directory /var/www/html. So, if a user visits example.com/index.html, Caddy serves the index.html file from that directory.

Caddy setup

There are only two things needed to get started with Caddy. That is Installation and Configuration

Installation

Caddy supports various installation methods tailored to different operating systems, including Windows, Ubuntu, and more, as well as options like raw binaries and Docker support.

Visit https://caddyserver.com/docs/install to choose the installation method that best suits your requirements.

For instance, if you’re using an Amazon Linux-based EC2 instance and prefer using binaries, you can download the static binary from their GitHub releases page and move it to the desired path for installation.

Configuration

This is the easiest part of Caddy. This is the main factor I choose Caddy over other software like nginx and apache.

Configuration plays a crucial role in defining how the web server should handle incoming HTTP requests and server content. There are two primary ways to do this. One is using Caddyfile and other is using JSON config. I am going to explain the Caddyfile now as it’s the better way.

The Caddyfile is a human-readable configuration file that uses a simple syntax for defining server directives. You just create a file with the name Caddyfile without any extension (just like Dockerfile).

# Caddyfile

www.candletower.com {
redir https://candletower.com{uri}
}

candletower.com {
reverse_proxy localhost:8080

handle_errors {
rewrite * /error.html
file_server
}
}

This is a simple Caddyfile which I use for my webserver.

  1. I redirect my www.candletower.com calls to my candletower.com
  2. Then I reverse proxy my candletower.com to my app service running on localhost with port 8080

As you can see, we can also specify an error page incase if we face any error codes. And that’s it.

Starting the server

There are only 4 commonly used caddy commands. They are

  1. caddy run : Starts the caddy server. Looks for the Caddyfile in the current directory, if not present then uses the default one.
  2. caddy start: Same as run but it starts the caddy server in the background as a process. Much preferred way.
  3. caddy stop: Used to stop the caddy service
  4. caddy reload: When you make changes to the Caddyfile, you can run this command to apply the changes to the running Caddy instance without causing any downtime.

caddy reload might not be required because caddy can automatically detect changes in Caddyfile and does the reload for you.

And that’s it. You can install Caddy, create your Caddyfile, and start the service in just a few minutes. It’s highly performant and has no learning curve.

Final Note

Thanks for reading my article. Hope you got the information that you are looking for. And please check out candletower.com

--

--