代理与负载均衡
forward-proxy、load-balancer、caching-server。
30 分钟
代理与负载均衡
生产流量 rarely 直连应用进程:Forward Proxy 管控出站、Reverse Proxy / Load Balancer 分发入站、Caching Server 卸载静态与 API 缓存。本课覆盖 /roadmaps/devops 的 forward-proxy、load-balancer、caching-server 节点,是 CodeRoadMap(Nginx 反代 Spring Boot + Next.js)平台化的关键一层,并与 kubernetes-essentials 的 Ingress/Service、devops-cloud-essentials 的云 LB 产品、dcp-setup 的架构设计衔接。
学习目标
- 区分 正向代理、反向代理、负载均衡、缓存 的职责与典型产品
- 能配置 Nginx / HAProxy 作为 CodeRoadMap 七层入口(TLS、路由、限流)
- 理解 L4 vs L7 负载均衡 及云托管 LB(ALB、App Gateway、Cloud Load Balancing)的差异
三类组件对照
| 类型 | 方向 | 典型产品 | 用途 |
|---|---|---|---|
| Forward Proxy | 客户端 → 外网 | Squid、企业网关 | 出站审计、缓存、突破内网限制 |
| Reverse Proxy / LB | 外网 → 后端 | Nginx、HAProxy、Traefik、云 ALB | TLS 终结、路由、健康检查 |
| Caching Server | 读多写少 | Varnish、Nginx proxy_cache、CDN | 降低 origin 压力、加速静态 |
Internet
→ Cloud LB / Nginx (TLS + L7 路由)
├─ / → platform (Next.js :3000)
├─ /api/* → backend (Spring Boot :8080)
└─ /static/* → CDN / object storage
→ Forward Proxy(仅内网 CI 访问 npm/Maven 外网时)
Nginx 反向代理配置
# /etc/nginx/conf.d/coderoadmap.conf
upstream backend {
least_conn;
server 10.0.1.10:8080 max_fails=3 fail_timeout=30s;
server 10.0.1.11:8080 max_fails=3 fail_timeout=30s;
keepalive 32;
}
upstream platform {
server 10.0.1.20:3000;
}
server {
listen 443 ssl http2;
server_name www.coderoadmap.example.com;
ssl_certificate /etc/nginx/ssl/fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/privkey.pem;
# 静态缓存
location /_next/static/ {
proxy_cache static_cache;
proxy_cache_valid 200 7d;
proxy_pass http://platform;
}
location /api/ {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
limit_req zone=api burst=20 nodelay;
}
location / {
proxy_pass http://platform;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}