场景:家里有一台树莓派,想在外面访问它的 Web 界面

问题是:家里的宽带没有公网 IP(或者有公网 IP 但运营商封了入站端口)。内网穿透就是在这种情况下让外部能访问到内网服务。

方案一:Cloudflare Tunnel(推荐,零成本)

不需要自己有公网 IP 的 VPS,Cloudflare 帮你中转。

# 在树莓派上安装 cloudflared
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-arm64 -o /usr/local/bin/cloudflared
chmod +x /usr/local/bin/cloudflared

# 登录并创建隧道
cloudflared tunnel login
cloudflared tunnel create my-tunnel

# 配置隧道指向本地服务
# config.yml
tunnel: TUNNEL_ID
credentials-file: /root/.cloudflared/TUNNEL_ID.json
ingress:
  - hostname: pi.mydomain.com
    service: http://localhost:8080
  - service: http_status:404

# 运行
cloudflared tunnel run my-tunnel

之后访问 pi.mydomain.com 就等于访问树莓派的 8080 端口。全程零端口开放。

方案二:frp(需要一台有公网 IP 的 VPS)

VPS 做服务端,内网设备做客户端。

# 服务端(VPS 上)
# frps.toml
bindPort = 7000
vhostHTTPPort = 8080

# 客户端(树莓派上)
# frpc.toml
serverAddr = "你的VPS的IP"
serverPort = 7000
[[proxies]]
name = "web"
type = "http"
localPort = 8080
customDomains = ["pi.yourdomain.com"]

选哪个?

  • 有域名 + 不想维护 VPS → Cloudflare Tunnel(最省心)
  • 需要 TCP/UDP 端口转发(比如远程桌面/SSH)→ frp
  • 流量大(CF Tunnel 免费版有隐性限速)→ frp