LIVE_STREAM_CONNECTED
Caddy 安装配置与使用指南
A
Mastermind
admin
Dimension
Linux
Timestamp
2025年12月21日
Caddy 是一个现代化的 Web 服务器,最大特点是自动 HTTPS,配置简单,非常适合快速部署。
安装
Ubuntu/Debian
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
CentOS/RHEL
sudo yum install yum-plugin-copr
sudo yum copr enable @caddy/caddy
sudo yum install caddy
Docker
docker run -d -p 80:80 -p 443:443 \
-v caddy_data:/data \
-v /path/to/Caddyfile:/etc/caddy/Caddyfile \
caddy
验证安装
caddy version
sudo systemctl status caddy
基础配置
配置文件位置:/etc/caddy/Caddyfile
静态网站
example.com {
root * /var/www/html
file_server
}
反向代理
example.com {
reverse_proxy localhost:3000
}
多站点
# 站点 1
example.com {
reverse_proxy localhost:3000
}
# 站点 2
api.example.com {
reverse_proxy localhost:8080
}
# 站点 3
blog.example.com {
root * /var/www/blog
file_server
}
常用配置
自动 HTTPS
Caddy 默认自动申请和续期 Let's Encrypt 证书,无需额外配置:
example.com {
reverse_proxy localhost:3000
}
# 自动获取 HTTPS 证书
强制 HTTPS
http://example.com {
redir https://example.com{uri} permanent
}
https://example.com {
reverse_proxy localhost:3000
}
负载均衡
example.com {
reverse_proxy localhost:3001 localhost:3002 localhost:3003 {
lb_policy round_robin
}
}
带路径的反向代理
example.com {
handle /api/* {
reverse_proxy localhost:8080
}
handle {
reverse_proxy localhost:3000
}
}
Gzip 压缩
example.com {
encode gzip
reverse_proxy localhost:3000
}
自定义头部
example.com {
header {
X-Frame-Options "SAMEORIGIN"
X-Content-Type-Options "nosniff"
X-XSS-Protection "1; mode=block"
-Server
}
reverse_proxy localhost:3000
}
日志配置
example.com {
log {
output file /var/log/caddy/access.log
format json
}
reverse_proxy localhost:3000
}
基础认证
# 生成密码哈希
caddy hash-password
example.com {
basicauth /admin/* {
admin $2a$14$...hashed_password...
}
reverse_proxy localhost:3000
}
WebSocket 支持
example.com {
reverse_proxy /ws localhost:3000 {
header_up Host {host}
header_up X-Real-IP {remote}
}
reverse_proxy localhost:3000
}
SPA 应用配置
Vue/React 单页应用
example.com {
root * /var/www/dist
encode gzip
try_files {path} /index.html
file_server
}
带 API 代理的 SPA
example.com {
handle /api/* {
reverse_proxy localhost:8080
}
handle {
root * /var/www/dist
try_files {path} /index.html
file_server
}
}
完整配置示例
# 全局配置
{
email admin@example.com
# 测试时可用 staging CA
# acme_ca https://acme-staging-v02.api.letsencrypt.org/directory
}
# 主站点
example.com {
encode gzip
# 安全头
header {
X-Frame-Options "SAMEORIGIN"
X-Content-Type-Options "nosniff"
Strict-Transport-Security "max-age=31536000; includeSubDomains"
}
# API 代理
handle /api/* {
reverse_proxy localhost:8080
}
# 静态文件
handle {
root * /var/www/html
try_files {path} /index.html
file_server
}
# 日志
log {
output file /var/log/caddy/example.log
format json
}
}
# API 子域名
api.example.com {
reverse_proxy localhost:8080
}
# 静态资源 CDN
static.example.com {
root * /var/www/static
file_server browse
header Cache-Control "public, max-age=31536000"
}
服务管理
# 启动
sudo systemctl start caddy
# 停止
sudo systemctl stop caddy
# 重启
sudo systemctl restart caddy
# 重载配置(不中断服务)
sudo systemctl reload caddy
# 开机自启
sudo systemctl enable caddy
# 查看状态
sudo systemctl status caddy
# 查看日志
sudo journalctl -u caddy -f
命令行工具
# 验证配置文件
caddy validate --config /etc/caddy/Caddyfile
# 格式化配置文件
caddy fmt --overwrite /etc/caddy/Caddyfile
# 直接运行(前台)
caddy run --config /etc/caddy/Caddyfile
# 后台运行
caddy start --config /etc/caddy/Caddyfile
# 停止
caddy stop
# 重载
caddy reload
证书管理
使用自定义证书
example.com {
tls /path/to/cert.pem /path/to/key.pem
reverse_proxy localhost:3000
}
内部/自签名证书
localhost {
tls internal
reverse_proxy localhost:3000
}
禁用 HTTPS
http://example.com {
reverse_proxy localhost:3000
}
常见问题
端口 80/443 被占用
# 查看占用
sudo lsof -i :80
sudo lsof -i :443
# 停止其他服务
sudo systemctl stop nginx
sudo systemctl stop apache2
证书申请失败
- 确保域名已解析到服务器
- 确保 80 和 443 端口开放
- 检查防火墙设置
sudo ufw allow 80
sudo ufw allow 443
配置不生效
# 验证配置
caddy validate --config /etc/caddy/Caddyfile
# 重载配置
sudo systemctl reload caddy
与 Nginx 对比
| 特性 | Caddy | Nginx |
|---|---|---|
| 自动 HTTPS | ✅ 内置 | ❌ 需配置 |
| 配置语法 | 简洁 | 复杂 |
| 性能 | 优秀 | 极致 |
| 学习成本 | 低 | 高 |
| 生态 | 较新 | 成熟 |
总结
Caddy 核心优势:
- 自动 HTTPS,零配置证书
- 配置文件简洁直观
- 热重载,无需重启
- 内置反向代理和负载均衡
适合快速部署、个人项目和中小型应用,大幅降低运维复杂度。