Auto-sync: 2026-04-22 19:20
This commit is contained in:
149
wiki/concepts/反向代理.md
Normal file
149
wiki/concepts/反向代理.md
Normal file
@@ -0,0 +1,149 @@
|
||||
---
|
||||
title: "反向代理"
|
||||
type: concept
|
||||
aliases: [Reverse Proxy, 反向代理服务器]
|
||||
tags: [network, proxy, infrastructure, web-server]
|
||||
---
|
||||
|
||||
# 反向代理
|
||||
|
||||
## Definition
|
||||
**反向代理**(Reverse Proxy)是一种服务器架构模式,代理服务器位于客户端与源服务器之间,代表源服务器接收客户端请求,并对请求进行转发、负载均衡或缓存。与正向代理(代理客户端)不同,反向代理对客户端透明,客户端不知道真实服务器的存在。
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────┐
|
||||
│ Reverse Proxy │
|
||||
│ (Caddy / Nginx / Apache) │
|
||||
│ │
|
||||
Client Request → │ example.com → localhost:8080 │
|
||||
(隐藏真实服务器) │ api.example.com → localhost:3000 │
|
||||
└──────────────┬──────────────────────┘
|
||||
│
|
||||
┌──────────────┼──────────────────────┐
|
||||
│ │ │
|
||||
▼ ▼ ▼
|
||||
┌──────────┐ ┌──────────┐ ┌──────────┐
|
||||
│ Server 1 │ │ Server 2 │ ... │ Server N │
|
||||
│ :8080 │ │ :3000 │ │ :5000 │
|
||||
└──────────┘ └──────────┘ └──────────┘
|
||||
```
|
||||
|
||||
## Common Use Cases
|
||||
|
||||
### 1. 多服务域名复用
|
||||
单台 VPS 通过不同子域名代理到不同内网服务:
|
||||
```
|
||||
nas.ishenwei.online → 127.0.0.1:15000 (NAS)
|
||||
n8n.ishenwei.online → 127.0.0.1:15678 (n8n)
|
||||
grafana.ishenwei.online → 127.0.0.1:13000 (Grafana)
|
||||
```
|
||||
|
||||
### 2. 自动 HTTPS
|
||||
反向代理自动处理 SSL 证书申请和续期:
|
||||
```
|
||||
Caddy: 自动从 Let's Encrypt 获取证书
|
||||
Nginx: 需要手动配置 certbot
|
||||
```
|
||||
|
||||
### 3. 负载均衡
|
||||
```
|
||||
upstream backend {
|
||||
server 192.168.1.10:8080;
|
||||
server 192.168.1.11:8080;
|
||||
server 192.168.1.12:8080;
|
||||
}
|
||||
```
|
||||
|
||||
### 4. 缓存加速
|
||||
静态资源缓存,减少源服务器负载:
|
||||
```
|
||||
location /static/ {
|
||||
proxy_cache_valid 200 60m;
|
||||
proxy_cache_use_stale error timeout updating;
|
||||
}
|
||||
```
|
||||
|
||||
## Comparison of Tools
|
||||
|
||||
| 工具 | 语言 | 配置复杂度 | 自动 HTTPS | 内存占用 | 适用场景 |
|
||||
|------|------|-----------|------------|----------|---------|
|
||||
| **Caddy** | Go | ⭐ 简单 | ✅ 内置 | ~20MB | 个人/小型服务 |
|
||||
| **Nginx** | C | ⭐⭐⭐ 中等 | 需配置 | ~5MB | 生产环境 |
|
||||
| **Traefik** | Go | ⭐⭐ 简单 | ✅ 内置 | ~50MB | 容器编排 |
|
||||
| **Apache** | C | ⭐⭐⭐⭐ 复杂 | 需配置 | ~50MB | 传统企业 |
|
||||
|
||||
## Caddy Configuration
|
||||
|
||||
### 基本反向代理
|
||||
```caddyfile
|
||||
example.com {
|
||||
reverse_proxy localhost:8080
|
||||
}
|
||||
```
|
||||
|
||||
### 多域名代理
|
||||
```caddyfile
|
||||
nas.ishenwei.online {
|
||||
reverse_proxy 127.0.0.1:15000
|
||||
}
|
||||
|
||||
n8n.ishenwei.online {
|
||||
reverse_proxy 127.0.0.1:15678
|
||||
}
|
||||
```
|
||||
|
||||
### 带路径重写
|
||||
```caddyfile
|
||||
example.com/api/* {
|
||||
rewrite /api
|
||||
reverse_proxy localhost:3000
|
||||
}
|
||||
```
|
||||
|
||||
### 带负载均衡
|
||||
```caddyfile
|
||||
example.com {
|
||||
reverse_proxy localhost:8080 localhost:8081 localhost:8082 {
|
||||
lb_policy round_robin
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Integration with frp
|
||||
|
||||
典型架构:frp 隧道 → 反向代理 → 自动 HTTPS
|
||||
|
||||
```
|
||||
┌──────────────────────────────────────────────────────────┐
|
||||
│ VPS │
|
||||
│ │
|
||||
│ Internet → :443 → Caddy (反向代理) → 127.0.0.1:15000 │
|
||||
│ ↓ │
|
||||
│ frps 监听 :15000 │
|
||||
│ ↓ │
|
||||
│ frp 隧道 │
|
||||
└──────────────────────────────────────────────────────────┘
|
||||
↓
|
||||
┌──────────────────────────────────────────────────────────┐
|
||||
│ 内网机器 │
|
||||
│ │
|
||||
│ frpc 连接 VPS:7000 │
|
||||
│ ↓ │
|
||||
│ frpc 映射 localhost:5000 → VPS:15000 │
|
||||
│ ↓ │
|
||||
│ NAS Web UI (5000端口) │
|
||||
└──────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
## Related Concepts
|
||||
- [[Caddy]] — 自动 HTTPS 的反向代理工具
|
||||
- [[内网穿透]] — 反向代理在内网服务访问中的应用
|
||||
- [[TCP 隧道]] — 反向代理的底层机制之一
|
||||
- [[Let's Encrypt]] — 自动 HTTPS 证书来源
|
||||
- [[负载均衡]] — 反向代理的高级功能
|
||||
|
||||
## References
|
||||
- Caddy: https://caddyserver.com/docs/
|
||||
- Nginx: https://nginx.org/en/docs/
|
||||
Reference in New Issue
Block a user