Files
myWiki/raw/articles/caddy-reverse-proxy-auth-2026.md

28 lines
1.3 KiB
Markdown
Raw Permalink 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.

# Caddy 反向代理认证方案
- **类型**: 技术教程/配置指南
- **来源**: 用户直接上传 TXT
- **整理日期**: 2026-04-29
- **标签**: #caddy #reverse-proxy #authentication #api-key #bearer-token
## 内容概要
涵盖 Caddy 反向代理场景下的 5 种认证方案,从纯内置到外部委托,按复杂度递增排列。
### 方案矩阵
| # | 方案 | 依赖 | 核心指令 | 适用场景 |
|---|------|------|---------|---------|
| 1 | Header 匹配器 | 无(纯内置) | `header` + `not` + `respond` | 固定 API Key / Bearer Token |
| 2 | Route + Handle | 无(纯内置) | `route` + `handle` | 部分路径需认证 |
| 3 | Basic Auth | 无(纯内置) | `basicauth` | 用户名密码保护 |
| 4 | Forward Auth | 外部认证服务 | `forward_auth` | 复杂认证逻辑查DB等 |
| 5 | JWT 插件 | 第三方编译 | `authp` + `jwt` | 完整 JWT 生态 |
## 关键技术点
- **命名匹配器 (Named Matcher)**: Caddy 的核心抽象,`@name { ... }` 定义条件组
- **逻辑否定**: `not header` 匹配不符合条件的请求("未认证则拒绝"的反向逻辑)
- **多 Key 白名单**: `@authorized` 收集多个允许的 Key`@unauthorized { not @authorized }` 拒绝不在白名单
- **forward_auth**: Caddy 原生支持外部认证委托(返回 200 通过401/403 拒绝)