Skip to content

06. 跨网穿透:FRP + 香港云 + 443 双域名神仙架构

家庭开发机部署好后,最大的阻碍在于:家庭宽带没有公网 IP,在公司或外出时如何丝滑连入?

本章记录了一套兼顾极低延迟(15ms)免 ICP 备案以及不破坏已有科学上网代理的高阶网络穿透架构。


一、服务器选型与流量架构

  • 中继节点:腾讯云香港轻量服务器(优质直连 BGP 线路,免工信部 ICP 备案,实测回国延迟仅 15ms);
  • 前置现状:香港服务器原本部署了 Xray 科学上网代理,独占了外网标准的 443 端口并挂载了伪装站点;
  • 核心诉求:既要利用香港服务器的 443 端口提供带 SSL 证书的 OpenCode 域名访问,又绝对不能破坏原有的 Xray VPN 服务。

二、FRP 内网穿透搭建

1. 服务端(香港机器 frps

/etc/frp/frps.toml 写入:

toml
bindPort = 7000
auth.token = "your_secret_frp_token"

启动服务:sudo systemctl enable --now frps。并在腾讯云安全组放行 70004096222233890 端口。

2. 客户端(家里 Ubuntu frpc

/etc/frp/frpc.toml 写入:

toml
serverAddr = "hk.your-domain.com"
serverPort = 7000
auth.token = "your_secret_frp_token"
loginFailExit = false # 自动断线重连

# 1. 穿透 OpenCode Web (4096)
[[proxies]]
name = "opencode-web"
type = "tcp"
localIP = "127.0.0.1"
localPort = 4096
remotePort = 4096

# 2. 穿透 Ubuntu 终端 SSH (22 -> 2222)
[[proxies]]
name = "ubuntu-ssh"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 2222

# 3. 穿透 Windows 11 远程桌面 RDP (3389 -> 33890)
[[proxies]]
name = "windows-rdp"
type = "tcp"
localIP = "192.168.31.216"
localPort = 3389
remotePort = 33890

启动客户端:sudo systemctl enable --now frpc


三、终极技术突破:Xray (REALITY) 与 Nginx 443 端口双域名共存

1. 踩坑现象

为 OpenCode 申请了新二级域名 code.your-domain.com,但在 Nginx 配置 listen 443 ssl; 后,打开网址依然返回 Xray 的旧伪装站,且提示证书不匹配。

2. 底层原因

运行 ss -tulpn 发现:443 端口实际由 xray 进程独占监听!Xray 采用 REALITY / 回落机制,将非科学上网流量自动转发给了本地 Nginx 的 127.0.0.1:8443。因此在 Nginx 里监听 443 是无效的!

3. 双域名 SNI 协同解法

在 DNS 后台将 code.your-domain.com 解析到香港服务器,使用 Certbot 申请证书,并让 Nginx 在 127.0.0.1:8443 同样挂上这套新规则:

nginx
# /etc/nginx/sites-available/opencode
server {
    listen 80;
    server_name code.your-domain.com;
    location / { return 301 https://$host$request_uri; }
}

server {
    listen 127.0.0.1:8443 ssl http2;
    server_name code.your-domain.com;

    ssl_certificate     /etc/letsencrypt/live/code.your-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/code.your-domain.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:4096;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_buffering off;
        proxy_read_timeout 86400s;
    }
}

启用并重载:

bash
sudo ln -sf /etc/nginx/sites-available/opencode /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

🎉 达成效果

  • 访问 https://hk.your-domain.com ➔ 显示伪装静态页,Xray 科学上网稳定如初;
  • 访问 https://code.your-domain.com ➔ 纯正 443 端口免端口号、全绿锁 SSL 证书,秒连 OpenCode!

四、动态开发端口的临时预览神器:Cloudflare Quick Tunnel

开发项目时,前端端口(如 30005173)或后端接口(8000)频繁变动,不可能每次都修改 FRP 和 Nginx。

安装 cloudflared 后,可随时按需生成带 HTTPS 的临时公网链接:

bash
cloudflared tunnel --url http://localhost:3000

3 秒内返回 https://xxxx.trycloudflare.com,手机和外网电脑均可立即点击预览页面,任务结束按 Ctrl+C 立即销毁!

基于 MIT 协议开源发布 | 配套 10 分钟实战视频