Files
nexus/wiki/concepts/Caddy.md
2026-04-27 16:26:34 +08:00

104 lines
3.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Caddy"
type: concept
tags: [networking, web-server, https, reverse-proxy, golang]
last_updated: 2026-04-03
---
## Aliases
- Caddy Server
- Caddy Web Server
- Caddyfile
## Definition
Caddy 是一款由 Go 语言编写的开源 Web 服务器,以其零配置自动 HTTPS 特性著称——首次访问域名时自动向 Let's Encrypt 申请并续期 SSL/TLS 证书无需手动配置。支持反向代理、静态文件服务、FastCGI、负载均衡等常用功能。Caddyfile 是其独特的配置格式,简洁易读,适合个人和小型部署。
## Core Features
- **自动 HTTPS**:首次访问自动申请 Let's Encrypt 证书,支持 ACME DNS-01 挑战(适合内网)
- **HTTP/3 支持**:默认启用 QUIC/HTTP3
- **反向代理**:简洁的 `reverse_proxy` 指令
- **Caddyfile**:人类可读的配置文件格式
- **默认 TLS 1.3**:安全开箱即用
- **热重载**:配置修改自动生效,无需重启
## Caddyfile 语法示例
```caddy
# 基础反向代理
example.com {
reverse_proxy localhost:8080
}
# 多域名配置
nas.ishenwei.online {
reverse_proxy 127.0.0.1:15000
}
n8n.ishenwei.online {
reverse_proxy 127.0.0.1:15678
}
# 带基础认证
admin.example.com {
basicauth /* {
admin <hashed_password>
}
reverse_proxy localhost:5678
}
```
## 常用命令
```bash
# 验证配置语法
sudo caddy validate --config /etc/caddy/Caddyfile
# 热重载配置
sudo caddy reload
# 彻底重启
sudo systemctl restart caddy
# 查看状态
sudo systemctl status caddy
# 生成密码哈希
caddy hash-password
```
## 与 Nginx 的对比
| 特性 | Caddy | Nginx |
|------|-------|-------|
| 自动 HTTPS | ✅ 默认 | ❌ 需手动配置 |
| 配置语法 | 简洁 Caddyfile | 复杂块结构 |
| 性能 | 略低于 Nginx | 极高 |
| 热重载 | ✅ 原生支持 | ❌ 需 signal |
| HTTP/3 | ✅ 默认 | 需额外编译 |
| 学习曲线 | 低 | 中高 |
## 在本 Wiki 中的应用
- [[通过VPS+内网反向代理实现域名访问内网穿透]]Caddy 反向代理到 frp 映射端口,提供 `*.ishenwei.online` HTTPS 访问
- 映射关系:
- `nas.ishenwei.online``127.0.0.1:15000`
- `n8n.ishenwei.online``127.0.0.1:15678`
- `transmission.ishenwei.online``127.0.0.1:19091`
- `grafana.ishenwei.online``127.0.0.1:13000`
- `navidrome.ishenwei.online``127.0.0.1:14533`
- `calibre.ishenwei.online``127.0.0.1:18083`
## 重要限制
- **Caddy 不处理 SSH**SSH 穿透TCP 映射)不走 Caddy只依赖 frps + frpc
- **Caddy 不处理 UDP**UDP 流量(如 DNS需要其他方案
## Related Concepts
- [[反向代理]]Caddy 是反向代理的具体实现
- [[内网穿透]]Caddy 通常与 frp 配合Caddy 提供 HTTPSfrp 建立隧道
- [[TCP隧道]]Caddy 处理 HTTP/HTTPS 层TCP 隧道Caddy 不参与)
## Related Entities
- [[RackNerd]]Caddy 部署的 VPS 提供商
- [[VPS]]:运行 Caddy 的公网服务器
## References
- 官网: https://caddyserver.com/
- 文档: https://caddyserver.com/docs/
- Caddyfile 语法: https://caddyserver.com/docs/caddyfile