Caddy

Basic usage of Caddy web server

Caddy is an extensible server platform written in Go. It automatically obtains and renews TLS certificates, making it a great choice for serving secure websites.
Caddy is much easier to configure than Nginx or Apache, and it has a simple configuration file format.

Installation

see Caddy installation guide. Caddy supports Linux, macOS, Windows.

In a Ubuntu system, you can check if Caddy is started with the following command:

1
sudo systemctl status caddy

Caddy should be running after installation. it should show the status as active (running).

To reload Caddy

1
sudo systemctl reload caddy

Visit your site at https://your-domain.com. If you see the Caddy welcome page, it means Caddy is serving your static website correctly.

Serving a Static Website

The Caddy welcome page already has instructions on how to serve a static website. The welcome index page is located at /usr/share/caddy on Ubuntu.

To serve your own static website, follow these steps:

  1. Point your domain’s A/AAAA DNS records at this machine.
  2. Upload your site’s files to /var/www/html.
  3. Edit your Caddyfile at /etc/caddy/Caddyfile:
  • a. Replace :80 with your domain name
  • b. Change the site root to /var/www/html
  1. Reload the configuration: systemctl reload caddy
  2. Visit your site!

Here is default content of Caddyfile at /etc/caddy/Caddyfile that you can modify to serve a static website:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
:80 {
# Set this path to your site's directory.
root * /usr/share/caddy

# Enable the static file server.
file_server

# Another common task is to set up a reverse proxy:
# reverse_proxy localhost:8080

# Or serve a PHP site through php-fpm:
# php_fastcgi localhost:9000
}

# Refer to the Caddy docs for more information:
# https://caddyserver.com/docs/caddyfile

Reverse Proxy

Caddy can also be used as a reverse proxy. This is useful if you want to serve multiple applications on the same domain or if you want to proxy requests to another server.

Update Caddyfile to include a reverse proxy configuration. For example, to proxy requests to a Node.js application running on port 3000, you can add the following to your Caddyfile:

1
2
3
example.com {
reverse_proxy localhost:3000
}

run systemctl reload caddy to apply the changes.