20260514:增加新内容

This commit is contained in:
2026-05-14 13:54:52 +08:00
parent 56c4d3ef7c
commit b116710e4c
294 changed files with 10682 additions and 255 deletions

View File

@@ -0,0 +1,46 @@
---
title: 反向代理认证 (Reverse Proxy Authentication)
created: 2025-04-15
updated: 2026-05-01
type: concept
tags: []
sources: []
---
# 反向代理认证 (Reverse Proxy Authentication)
**在反向代理层拦截和验证请求身份的安全模式**,将认证逻辑从应用层上提到网关层。
## 核心优势
1. **关注点分离**: 后端服务无需处理认证逻辑,专注业务
2. **统一入口**: 多个后端共享同一套认证机制
3. **减少延迟**: 未认证请求在网关层即被拒绝,不到达后端
4. **配置集中**: 认证规则在反向代理配置中统一管理
## 在 Caddy 中的实现
[[caddy-web-server|Caddy]] 的 `header` 匹配器 + `respond` 指令构成了最简洁的反向代理认证方案:
```caddy
@unauthorized {
not header X-API-Key "secret-key"
}
respond @unauthorized "Unauthorized" 401
reverse_proxy localhost:8080
```
核心逻辑:定义"未授权"条件 → 拦截并返回 401 → 其余请求放行到后端。
## 扩展模式
- **路径级控制**: `route` + `handle` 对不同路径施加不同认证策略
- **多 Key 白名单**: 命名匹配器组合多个允许的 Key
- **委托认证**: [[forward-authentication|forward_auth]] 将认证决策交给外部服务
## 相关概念
- [[api-key-authentication]] — API Key / Token 的具体认证机制
- [[forward-authentication]] — 外部委托认证
- [[caddy-web-server]] — Caddy 实现
- [[caddy-reverse-proxy-auth]] — 完整配置指南