How to Use Caddy

· 1 min read · 182 Words · -Views -Comments

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:

brew install caddy

On CentOS7:

yum install yum-plugin-copr
yum copr enable @caddy/caddy
yum install caddy

On Ubuntu:

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

# Start service
caddy start --config ./Caddyfile

# Format config file
caddy fmt --overwrite

# Stop service
caddy stop

Examples

Reverse Proxy for Local Service

127.0.0.1:3001 {
    route /xyz* {
        uri strip_prefix /xyz
        reverse_proxy * 127.0.0.1:9000
    }
}

Reverse Proxy for External Service

example.com {
    reverse_proxy /v1/chat/completions https://api.openai.com {
        header_up Host {upstream_hostport}
    }
}

Serve Static Assets

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

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover