域名转发架构文档

域名转发架构文档

域名转发架构文档

从公网到家庭服务的完整转发链路分析

目录


系统架构概览

整个系统采用双层 Nginx 反向代理架构,实现了从公网到家庭局域网服务的安全访问:

  • 第一层:公网 VPS 上的 Nginx
  • 第二层:家庭网络内的 Nginx
graph TB subgraph Internet["公网域名层"] A1[example.com<br/>主站] A2[service1.example.com<br/>服务1控制台] A3[service2.example.com<br/>服务2] A4[service3.example.com<br/>服务3 API] end subgraph VPS["VPS Nginx<br/>公网服务器"] B1[Nginx:443<br/>SSL终端] B2[博客服务<br/>:8090] B3[301重定向<br/>节省带宽] end subgraph DDNS["DDNS域名层<br/>动态IP"] C1[ddns.example.com] C2[service1.ddns.example.com] C3[service2.ddns.example.com] C4[service3.ddns.example.com] end subgraph Home["家庭网络<br/>192.168.x.0/24"] D1[内网网关 Nginx<br/>:8443 SSL] D2[服务1 Console<br/>:9001] D3[服务3 API<br/>:9000] D4[服务2<br/>:5667 HTTPS] end A1 -->|HTTPS| B1 B1 -->|proxy_pass| B2 A2 -->|HTTPS| B3 A3 -->|HTTPS| B3 A4 -->|HTTPS| B3 B3 -.->|301 Redirect| C2 B3 -.->|301 Redirect| C3 B3 -.->|301 Redirect| C4 C2 -->|HTTPS:8443| D1 C3 -->|HTTPS:8443| D1 C4 -->|HTTPS:8443| D1 D1 -->|proxy_pass| D2 D1 -->|proxy_pass| D3 D1 -->|proxy_pass| D4 style B2 fill:#e1f5ff style B3 fill:#fff3e0 style D1 fill:#f3e5f5 style D2 fill:#e8f5e9 style D3 fill:#e8f5e9 style D4 fill:#e8f5e9

架构优势

特性 说明
带宽优化 大文件访问直连家庭网络,节省 VPS 流量费用
灵活性 支持动态 IP,家庭网络 IP 变化不影响服务
安全性 双层 SSL 加密,VPS 作为入口防火墙
成本效益 VPS 只需处理轻量服务和重定向

域名层级结构

二级域名(*.example.com)- VPS 公网层

graph LR subgraph "公网域名 - VPS Nginx" A[example.com] B[service1.example.com] C[service2.example.com] D[service3.example.com] E[*.example.com] end A -->|直接服务| F[博客服务<br/>VPS本地:8090] B -->|301重定向| G[节省VPS带宽] C -->|301重定向| G D -->|301重定向| G E -->|404| H[未配置子域名] style F fill:#4caf50,color:#fff style G fill:#ff9800,color:#fff style H fill:#f44336,color:#fff

证书配置

  • 证书类型: *.example.com 泛域名证书
  • 证书路径: /ssl/example.com.{crt,key}
  • 续期方式: acme.sh 自动续期
  • 有效期: 90天(30天前自动续期)

三级域名(*.ddns.example.com)- 家庭网络层

graph LR subgraph "DDNS域名 - 家庭内网网关 Nginx" A[service1.ddns.example.com] B[service2.ddns.example.com] C[service3.ddns.example.com] D[*.ddns.example.com] end A -->|proxy_pass| E[服务1 Console<br/>192.168.x.x:9001] B -->|proxy_pass| F[服务2<br/>192.168.x.x:5667] C -->|proxy_pass| G[服务3 API<br/>192.168.x.x:9000] D -->|404| H[未配置子域名] style E fill:#00bcd4,color:#fff style F fill:#9c27b0,color:#fff style G fill:#3f51b5,color:#fff style H fill:#f44336,color:#fff

证书配置

  • 证书类型: *.ddns.example.com 泛三级域名证书
  • 证书路径: /etc/nginx/ssl/ddns.example.com/
  • 续期方式: acme.sh DNS API 自动续期
  • OCSP Stapling: 已启用,提升 SSL 握手性能

转发流程详解

流程 1: 主站访问(博客服务)

sequenceDiagram participant User as 用户浏览器 participant VPS as VPS Nginx<br/>(公网) participant Blog as 博客服务<br/>:8090 User->>VPS: ① HTTP请求<br/>http://example.com VPS->>User: ② 301重定向<br/>https://example.com User->>VPS: ③ HTTPS请求<br/>https://example.com Note over VPS: SSL终止<br/>证书: *.example.com VPS->>Blog: ④ proxy_pass<br/>http://ddns.example.com:8090 Blog->>VPS: ⑤ 响应内容 VPS->>User: ⑥ 返回HTTPS响应 Note over User,Blog: 全程通过VPS,消耗VPS带宽

配置文件: example.com.conf

关键配置:

location / {
    proxy_pass http://ddns.example.com:8090;
    # WebSocket 支持
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

流程 2: 大文件服务访问(带宽优化型)

sequenceDiagram participant User as 用户浏览器 participant VPS as VPS Nginx<br/>(公网) participant Home as 内网网关 Nginx participant Service as 后端服务<br/>192.168.x.x User->>VPS: ① HTTPS请求<br/>https://service1.example.com Note over VPS: SSL终止<br/>证书: *.example.com VPS->>User: ② 301永久重定向<br/>https://service1.ddns.example.com:8443 Note over User: 浏览器自动跳转 User->>Home: ③ HTTPS请求<br/>https://service1.ddns.example.com:8443 Note over Home: SSL终止<br/>证书: *.ddns.example.com<br/>OCSP Stapling Home->>Service: ④ proxy_pass<br/>http://192.168.x.x:9001 Service->>Home: ⑤ 响应内容 Home->>User: ⑥ 返回HTTPS响应 Note over User,Service: VPS仅处理首次重定向<br/>后续流量直连家庭网络

VPS 配置: service1.example.com.conf

server {
    listen 443 ssl http2;
    server_name service1.example.com;

    # 永久重定向到DDNS域名
    return 301 https://service1.ddns.example.com:8443$request_uri;
}

家庭网络配置: service1.ddns.example.com.conf

server {
    listen 8443 ssl http2;
    server_name service1.ddns.example.com;

    location / {
        proxy_pass http://192.168.x.x:9001;
        # 大文件上传支持
        client_max_body_size 1000m;
        proxy_request_buffering off;
    }
}

流程 3: 未配置域名的处理

graph TD A[用户访问<br/>test.example.com] --> B{VPS Nginx<br/>匹配规则} B -->|匹配 *.example.com| C[wildcard-redirects.conf] C --> D{检查排除列表} D -->|已配置服务| E[返回444<br/>关闭连接] D -->|其他子域名| F[返回404<br/>Subdomain not configured] style E fill:#f44336,color:#fff style F fill:#ff9800,color:#fff

配置文件: wildcard-redirects.conf


服务配置矩阵

VPS 公网层

域名 端口 SSL证书 转发目标 类型 配置文件
example.com 443 *.example.com ddns.example.com:8090 反向代理 example.com.conf
service1.example.com 443 *.example.com service1.ddns.example.com:8443 301重定向 service1.example.com.conf
service2.example.com 443 *.example.com service2.ddns.example.com:8443 301重定向 service2.example.com.conf
service3.example.com 443 *.example.com service3.ddns.example.com:8443 301重定向 service3.example.com.conf
*.example.com 443 *.example.com N/A 404兜底 wildcard-redirects.conf

家庭网络层

域名 端口 SSL证书 转发目标 后端服务 配置文件
service1.ddns.example.com 8443 *.ddns.example.com 192.168.x.x:9001 服务1 Console service1.ddns.example.com.conf
service2.ddns.example.com 8443 *.ddns.example.com 192.168.x.x:5667 服务2 (HTTPS) service2.ddns.example.com.conf
service3.ddns.example.com 8443 *.ddns.example.com 192.168.x.x:9000 服务3 API service3.ddns.example.com.conf
*.ddns.example.com 8443 *.ddns.example.com N/A 404兜底 wildcard.ddns.example.com.conf

安全策略

SSL/TLS 配置

graph TB subgraph "SSL安全层次" A[TLS 1.2/1.3<br/>协议版本] B[HIGH:!aNULL:!MD5<br/>加密套件] C[HSTS<br/>强制HTTPS] D[OCSP Stapling<br/>证书验证] end subgraph "HTTP安全头" E[X-Frame-Options<br/>防止点击劫持] F[X-Content-Type-Options<br/>防止MIME嗅探] G[X-XSS-Protection<br/>XSS防护] end A --> H[安全传输层] B --> H C --> H D --> H E --> I[应用安全层] F --> I G --> I H --> J[多层防护体系] I --> J style H fill:#4caf50,color:#fff style I fill:#2196f3,color:#fff style J fill:#9c27b0,color:#fff

共同安全配置

所有配置文件都包含以下安全措施:

  1. 强制 HTTPS

    # HTTP自动跳转HTTPS
    return 301 https://$server_name$request_uri;
    
  2. HSTS 头部

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    
  3. 安全头部

    add_header X-Frame-Options SAMEORIGIN always;
    add_header X-Content-Type-Options nosniff always;
    add_header X-XSS-Protection "1; mode=block" always;
    
  4. OCSP Stapling(仅家庭网络层)

    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8 8.8.4.4 valid=300s;
    

访问控制策略

策略 实现方式 作用
白名单机制 通配符配置排除已配置域名 防止未授权子域名访问
连接关闭 返回 444 状态码 无响应关闭,节省资源
404 提示 返回明确错误信息 友好的错误提示

性能优化策略

1. 带宽优化架构

graph LR subgraph "轻量级流量 - 经过VPS" A1[博客访问] A2[首次重定向] end subgraph "大文件流量 - 直连家庭" B1[服务1文件上传/下载] B2[服务2大文件传输] B3[服务3 API数据传输] end A1 --> C[VPS带宽消耗] A2 --> C B1 --> D[家庭带宽消耗] B2 --> D B3 --> D C --> E[低成本VPS套餐] D --> F[家庭宽带无限流量] style C fill:#ff9800,color:#fff style D fill:#4caf50,color:#fff style E fill:#2196f3,color:#fff style F fill:#9c27b0,color:#fff

成本分析:

  • VPS 流量费用: ~$0.01/GB
  • 家庭宽带: 包月无限流量
  • 节省: 大文件传输完全绕过 VPS

2. SSL 性能优化

优化项 配置 性能提升
Session 缓存 ssl_session_cache shared:SSL:10m 减少重复握手
Session 超时 ssl_session_timeout 10m 会话复用
OCSP Stapling ssl_stapling on 减少客户端延迟 20-30ms
HTTP/2 listen 443 ssl http2 多路复用,减少连接数

3. 大文件传输优化

大文件服务 API 专用配置 (service3.ddns.example.com.conf):

# 禁用缓冲,支持流式传输
proxy_request_buffering off;
proxy_buffering off;

# 大文件超时
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;

# Range请求支持(断点续传)
proxy_set_header Range $http_range;
proxy_set_header If-Range $http_if_range;

# CORS跨域支持
add_header Access-Control-Allow-Origin * always;

4. WebSocket 支持

所有需要实时通信的服务都启用了 WebSocket:

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

适用服务:

  • 博客服务(实时编辑)
  • 服务1 Console(实时日志)
  • 服务2(实时同步)

证书管理策略

证书更新时间线

gantt title 证书生命周期管理 dateFormat YYYY-MM-DD section 公网层证书(*.example.com) 证书有效期 :done, old1, 2025-11-30, 90d 自动续期窗口 :active, old2, 2026-01-29, 1d section 内网层证书(*.ddns.example.com) DNS验证申请 :crit, new1, 2025-12-06, 1d 证书部署测试 :new2, 2025-12-07, 2d 证书有效期 :new3, 2025-12-09, 90d 自动续期窗口 :new4, 2026-03-09, 1d

acme.sh 自动续期配置

Cron 任务:

55 16 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null

续期命令:

# 公网层证书
~/.acme.sh/acme.sh --renew -d example.com

# 内网层证书
~/.acme.sh/acme.sh --renew -d ddns.example.com

证书共存策略

graph TB A[当前状态] --> B{证书管理} B --> C[公网层证书<br/>*.example.com] B --> D[内网层证书<br/>*.ddns.example.com] C --> E[VPS公网层] D --> F[家庭网络层] E --> G[博客/重定向服务] F --> H[大文件服务] G --> I[双证书共存<br/>互不影响] H --> I I --> J{新证书稳定后} J -->|可选| K[删除旧证书<br/>~/.acme.sh --remove] J -->|推荐| L[保留旧证书<br/>作为备份] style C fill:#ff9800,color:#fff style D fill:#4caf50,color:#fff style I fill:#2196f3,color:#fff style L fill:#9c27b0,color:#fff

故障排查指南

常见问题诊断流程

graph TD A[访问异常] --> B{错误类型} B -->|502 Bad Gateway| C[后端服务检查] B -->|SSL错误| D[证书检查] B -->|404 Not Found| E[域名配置检查] B -->|超时| F[网络连通性检查] C --> C1[检查后端服务状态] C1 --> C2[验证端口监听] C2 --> C3[查看服务日志] D --> D1[验证证书有效期] D1 --> D2[检查证书路径] D2 --> D3[测试OCSP Stapling] E --> E1[确认DNS解析] E1 --> E2[检查Nginx配置] E2 --> E3[nginx -t 测试] F --> F1[ping DDNS域名] F1 --> F2[检查防火墙规则] F2 --> F3[验证端口转发] style C fill:#f44336,color:#fff style D fill:#ff9800,color:#fff style E fill:#2196f3,color:#fff style F fill:#9c27b0,color:#fff

检查命令速查

# 1. 证书检查
openssl x509 -in /etc/nginx/ssl/ddns.example.com/fullchain.pem -noout -dates
~/.acme.sh/acme.sh --list

# 2. Nginx配置测试
sudo nginx -t
sudo nginx -s reload

# 3. 端口监听检查
sudo netstat -tlnp | grep nginx
sudo netstat -tlnp | grep 9001  # 服务1 Console
sudo netstat -tlnp | grep 9000  # 服务3 API

# 4. 服务日志
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/nginx/access.log

# 5. DNS解析测试
dig service1.ddns.example.com
nslookup service2.ddns.example.com

# 6. HTTPS连通性测试
curl -I https://service1.ddns.example.com:8443
curl -I https://service3.ddns.example.com:8443

部署清单

VPS 公网服务器

  • 安装 Nginx
  • 配置 *.example.com 泛域名证书
  • 部署公网层配置文件
  • 测试 HTTP 到 HTTPS 重定向
  • 验证 301 重定向功能
  • 测试博客服务访问

家庭内网网关

  • 安装 Nginx
  • 安装 acme.sh
  • 配置 DNS API
  • 申请 *.ddns.example.com 证书
  • 部署内网层配置文件
  • 配置防火墙(开放 8443 端口)
  • 测试所有服务访问
  • 验证 OCSP Stapling

网络层

  • 配置路由器端口转发(8443 → 内网网关)
  • 设置 DDNS 自动更新
  • 测试外网访问(使用移动网络测试)

附录

端口映射表

端口 服务 位置 协议
80 HTTP (重定向) VPS / 内网网关 HTTP
443 HTTPS (公网层) VPS HTTPS
8443 HTTPS (内网层) 内网网关 HTTPS
8090 博客服务 VPS本地 / DDNS HTTP
9000 服务3 API 192.168.x.x HTTP
9001 服务1 Console 192.168.x.x HTTP
5667 服务2 192.168.x.x HTTPS

相关文档

  • ACME 证书配置说明
  • Nginx 公网层配置文件
  • Nginx 内网层配置文件

文档版本: 1.0
更新日期: 2025-12-06
作者: 系统架构分析
状态: 生产环境

Apache Paimon Partial-Update 2025-12-12
Apache Paimon 表模式(Table Mode)详解 2025-12-04

评论区