Files
nexus/wiki/entities/Caddy.md

137 lines
3.3 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: entity
aliases: [Caddy Web Server, Caddy反代]
tags: [web-server, reverse-proxy, https, open-source]
---
# Caddy
## Overview
**Caddy** 是一个用 Go 语言编写的开源 Web 服务器,以自动 HTTPS 和简洁配置著称。相比 NginxCaddy 默认启用 HTTPSLet's Encrypt 自动证书),配置语法更简洁直观。
## Core Features
| 特性 | 说明 |
|------|------|
| **自动 HTTPS** | 自动从 Let's Encrypt 申请和续期 SSL 证书 |
| **自动 HTTP→HTTPS 重定向** | 无需手动配置 |
| **TLS 1.3 支持** | 现代加密标准 |
| **配置热加载** | 修改配置无需重启服务 |
| **反向代理** | 支持 HTTP/2、WebSocket |
| **Markdown 渲染** | 内置静态文件服务 |
## Installation (Ubuntu/Debian)
```bash
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
```
## Basic Configuration (Caddyfile)
### 简单反向代理
```
n8n.ishenwei.online {
reverse_proxy 127.0.0.1:15678
}
```
### 多域名配置
```
nas.ishenwei.online {
reverse_proxy 127.0.0.1:15000
}
grafana.ishenwei.online {
reverse_proxy 127.0.0.1:13000
}
```
### 带认证的反向代理
```
n8n.ishenwei.online {
basicauth /* {
admin JDJhJDE0JDN3ZXVhV2YyZG9SY2hvYzVmZ2h3QUlVblpOMU4vS1ptcENrSlhySElMb3l5dytOMkh0Tk93
}
reverse_proxy 127.0.0.1:15678
}
```
## Integration with frp
典型架构frp 建立内网隧道 → Caddy 反向代理到本地端口 → 自动 HTTPS
```
用户请求 https://n8n.ishenwei.online
阿里云 DNS → VPS 公网 IP
Caddy (443端口) 接收请求
Caddyfile 配置匹配 n8n.ishenwei.online
reverse_proxy 127.0.0.1:15678
frpc 在 VPS 15000 端口监听
frp 隧道 → 内网 Ubuntu 5678 端口
n8n 服务
```
## Common Commands
```bash
# 验证配置文件语法
sudo caddy validate --config /etc/caddy/Caddyfile
# 重载配置(热加载)
sudo systemctl reload caddy
# 重启服务
sudo systemctl restart caddy
# 查看状态
sudo systemctl status caddy
# 紧急恢复(服务卡死时)
sudo systemctl stop caddy
sudo pkill -9 caddy
sudo systemctl start caddy
```
## Troubleshooting
### Caddyfile 语法检查
```bash
sudo caddy validate --config /etc/caddy/Caddyfile
# 输出 "Valid configuration" 表示语法正确
```
### 端口被占用
如果 Caddy 启动失败,检查端口是否被占用:
```bash
ss -ltnp | grep ':80\|:443'
```
### Caddy 意外占用端口
某些一键脚本可能配置 Caddy 监听非标准端口,检查是否有:
```
:7000 {
reverse_proxy ...
}
```
## Related Concepts
- [[反向代理]] — Caddy 的核心功能
- [[Let's Encrypt]] — Caddy 自动使用的 SSL 证书提供商
- [[frp]] — Caddy 常与 frp 配合使用
- [[VPS]] — Caddy 通常部署在公网 VPS
## References
- 官网: https://caddyserver.com/
- 文档: https://caddyserver.com/docs/