References
安装
Binary
- 下载 binary:https://caddyserver.com/download
- 运行命令
sudo mv caddy /usr/bin/
sudo mkdir /etc/caddy
sudo cp Caddyfile /etc/caddy/Caddyfile
sudo groupadd --system caddy
sudo useradd --system \
--gid caddy \
--create-home \
--home-dir /var/lib/caddy \
--shell /usr/sbin/nologin \
--comment "Caddy web server" \
caddy
- 将以下内容写入 /etc/systemd/system/caddy.service 文件
[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target
[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile --force
TimeoutStopSec=5s
LimitNOFILE=1048576
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
[Install]
WantedBy=multi-user.target
- 启动
sudo systemctl daemon-reload
sudo systemctl enable --now caddy
sudo systemctl status caddy
注意:centos 7 的 systemd 版本太低(219),需要先升级
wget https://copr.fedorainfracloud.org/coprs/jsynacek/systemd-backports-for-centos-7/repo/epel-7/jsynacek-systemd-backports-for-centos-7-epel-7.repo -O /etc/yum.repos.d/jsynacek-systemd-centos-7.repo
sudo yum update systemd -y
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
# verify that the service is running
sudo systemctl status caddy
安装完后修改 Caddyfile sudo vi /etc/caddy/Caddyfile
,加载配置
sudo systemctl reload caddy
Docker
docker run -d --name caddy --restart unless-stopped \
-p 80:80 \
-v $PWD/Caddyfile:/etc/caddy/Caddyfile \
-v caddy_data:/data \
caddy
使用
运行
caddy run
# 指定 Caddyfile
caddy run --config /path/to/Caddyfile
# serve static files
caddy file-server --listen :2015 --root ~/mysite
# file browser
caddy file-server --browse
# reverse proxy
caddy reverse-proxy --from :2080 --to :9000
# format Caddyfile
caddy fmt --overwrite
API
查看配置
curl localhost:2019/config/
添加 JSON 配置
curl localhost:2019/load \
-H "Content-Type: application/json" \
-d @caddy.json
更新 caddyfile
curl localhost:2019/load \
-H "Content-Type: text/caddyfile" \
--data-binary @Caddyfile
Caddyfile
简单响应
:2015
respond "Hello, world!"
file server
localhost
root * /var/www/mysite
file_server
file browser
localhost
root * /var/www/mysite
file_server browse
revserse proxy
:2080
reverse_proxy :9000
auto https
your_domain.com:8443
reverse_proxy :9000
https with ip
your_ip:8443
reverse_proxy :9000
disable HTTP-to-HTTPS redirects
{
auto_https disable_redirects
}
your_domain.com:8443
reverse_proxy :9000
multiple sites
localhost:2015 {
respond "Hello, world!"
}
localhost:2016 {
respond "Goodbye, world!"
}
matcher token:只代理子路径
localhost
reverse_proxy /api/* 127.0.0.1:9005
handle_path
example.com {
handle_path /foo/* {
# The path has the "/foo" prefix stripped
}
handle /bar/* {
# The path still retains "/bar"
}
}
Log
example.com {
log {
output file /var/log/access.log {
roll_size 1gb
roll_keep 5
roll_keep_for 720h
}
}
}
示例
使用子路径代理 n8n 服务
n8n:添加 N8N_PATH
环境变量
docker run -d \
--name n8n \
-p 5678:5678 \
-v n8n_data:/home/node/.n8n \
-e TZ="Asia/Shanghai" \
-e N8N_PATH=/n8n/ \
-e N8N_SECURE_COOKIE=false \
docker.n8n.io/n8nio/n8n
caddy:通过 handle_path 处理子路径请求并改写路径
{
auto_https disable_redirects
}
9.135.143.173:8443 {
handle_path /n8n/* {
reverse_proxy localhost:5678
}
handle {
reverse_proxy localhost:8080
}
}