在构建个人服务时,我们常常需要将 Apache 提供的本地服务通过公网访问,并启用 HTTPS 进行加密通信。本文记录了如何:
- 将旧域名切换为新域名
- 将 HTTPS 证书管理迁移到反向代理服务器
- 使用 FRP 将内网服务穿透到公网
- 实现高效、安全、模块化的 HTTPS 架构
🧱 架构概览
本次部署采用了以下结构:
arduinoCopyEdit公网用户
↓
VPS(运行 FRP Server,负责公网端口)
↓
FRP 客户端 → 反向代理服务器(Nginx + Certbot,TLS 终结)
↓
内网 Apache 主机,仅提供 HTTP 服务(80端口)
- 域名:
new.example.com - 后端 Apache 不处理 HTTPS,仅监听 80 端口
- 证书签发与 TLS 加密完全由反代服务器完成
- FRP 将反代服务器的 443(加密)端口映射到 VPS 的公网端口(已脱敏)
🛰️ FRP 配置
FRP 客户端(运行在反代服务器)
配置如下(已脱敏):
iniCopyEdit[web_https]
type = tcp
local_ip = 127.0.0.1
local_port = 443
remote_port = xxxx # 例如 VPS 映射的 1443 端口
启动方式可为 systemd 服务或前台运行:
bashCopyEdit./frpc -c frpc.ini
FRP 服务端(运行在公网 VPS)
确保配置中开放了对应端口(如 remote_port = xxxx),并已通过防火墙放行。
🔐 在反向代理服务器上签发证书
Apache 后端不直接处理证书,因此在反向代理服务器上使用 standalone 模式手动申请:
bashCopyEditsudo certbot certonly --standalone -d new.example.com
此命令会:
- 使用临时监听端口(默认 80)验证域名所有权
- 签发 Let’s Encrypt 证书至
/etc/letsencrypt/live/new.example.com/
如端口被占用,可加参数 --http-01-port 8888 替代默认端口。
⚙️ Nginx 配置反代到 Apache
在反代服务器上配置 Nginx 终结 TLS,并转发流量到内网 Apache 主机:
nginxCopyEditserver {
listen 443 ssl;
server_name new.example.com;
ssl_certificate /etc/letsencrypt/live/new.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/new.example.com/privkey.pem;
location / {
proxy_pass http://192.168.x.x:80; # Apache 后端地址
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
启用配置并重启 Nginx:
bashCopyEditsudo ln -s /etc/nginx/sites-available/new.example.com /etc/nginx/sites-enabled/
sudo systemctl reload nginx
🧹 清理旧域名的证书
在原主机上如仍保留旧证书(如 old.example.com),可用 Certbot 删除:
bashCopyEditsudo certbot delete --cert-name old.example.com
Certbot 会自动清理 /etc/letsencrypt/live/, /archive/, /renewal/ 中对应项。
✅ 验证效果
从任意设备访问:
bashCopyEdithttps://new.example.com:xxxx
应看到已部署的 Web 服务内容,使用的是 Let’s Encrypt 证书,HTTPS 加密生效。
📌 总结
通过将 HTTPS 加密工作前置到反向代理服务器,并结合 FRP 实现公网映射,本方案具备以下优势:
- 🔐 后端主机简化部署,无需处理证书
- 🔄 域名替换、证书续期可独立完成
- 🧩 模块化结构,利于多服务统一管理
- 🌍 可结合任意 VPS 实现全球公网访问