Caddy 反向代理与自动 HTTPS
Caddy 作为生产入口:自动申请 Let's Encrypt 证书、反向代理到 nginx、安全头配置。
#type / howto
#status / growing
#tech / ops
#resource / caddy
[!info] related notes
- 部署: Docker Compose
Caddy 反向代理与自动 HTTPS
为什么选 Caddy
Caddy 的核心卖点:给定域名后自动申请、续期、配置 HTTPS 证书,零配置。
| 维度 | Caddy | nginx |
|---|---|---|
| HTTPS | 自动 | 手动 certbot |
| 配置 | Caddyfile(简洁) | nginx.conf(复杂) |
| 性能 | 略低 | 最高 |
Caddyfile
{$APP_DOMAIN:example.com} {
reverse_proxy web:80
header {
Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
X-Content-Type-Options "nosniff"
-Server
}
}
Caddy 自动:申请证书、HTTP→HTTPS 重定向、续期。
Docker 集成
caddy:
image: caddy:2-alpine
ports:
- '80:80'
- '443:443'
- '443:443/udp' # HTTP/3
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile:ro
- caddy-data:/data # 证书持久化
请求流向
浏览器 → Caddy (HTTPS) → nginx (静态+API代理) → Go 后端 → AI 服务
没有域名时
只有 IP 时无法申请 Let’s Encrypt。可以 HTTP 直接访问或自签证书。