How to Use Caddy

Caddy is simpler compared to Nginx and very convenient for server proxying, especially for obtaining HTTPS certificates.

Here are some common operations recorded.

Installation

It is recommended to use a package manager for installation, such as on Mac:

1
brew install caddy

On CentOS7:

1
2
3
yum install yum-plugin-copr
yum copr enable @caddy/caddy
yum install caddy

On Ubuntu:

1
2
3
4
5
sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
sudo apt update
sudo apt install caddy

For installation on other OS, see https://caddyserver.com/docs/install#install

Common Commands

1
2
3
4
5
6
7
8
# Start service
caddy start --config ./Caddyfile

# Format config file
caddy fmt --overwrite

# Stop service
caddy stop

Examples

Reverse Proxy for Local Service

1
2
3
4
5
6
127.0.0.1:3001 {
route /xyz* {
uri strip_prefix /xyz
reverse_proxy * 127.0.0.1:9000
}
}

Reverse Proxy for External Service

1
2
3
4
5
example.com {
reverse_proxy /v1/chat/completions https://api.openai.com {
header_up Host {upstream_hostport}
}
}

Serve Static Assets

1
2
3
4
1991421.cn {
root * /var/www/blog/public
file_server
}

At the end

That’s all I know. I recommend learning from the official doc for more information on how to use it.

https://caddyserver.com/docs/caddyfile