Files
nexus/wiki/concepts/反向代理.md
2026-04-22 19:20:32 +08:00

5.7 KiB
Raw Blame History

title, type, aliases, tags
title type aliases tags
反向代理 concept
Reverse Proxy
反向代理服务器
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

基本反向代理

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
}

带路径重写

example.com/api/* {
    rewrite /api
    reverse_proxy localhost:3000
}

带负载均衡

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端口)                                   │
└──────────────────────────────────────────────────────────┘

References