侧边栏壁纸
博主头像
怪客のBlog 博主等级

行动起来,活在当下

  • 累计撰写 37 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

Windows 上使用 Cloudflare Tunnel内网穿透

怪客
2026-02-10 / 0 评论 / 0 点赞 / 22 阅读 / 0 字

Windows 上使用 Cloudflare Tunnel 完整教程


一、前置准备

  • ✅ 一个 Cloudflare 账号
  • ✅ 域名已托管在 Cloudflare
  • ✅ Windows 10/11 电脑

二、两种方式(推荐方式一)


方式一:通过 Zero Trust 网页面板(最简单 ⭐推荐)

步骤 1:创建隧道

  1. 登录 Cloudflare Zero Trust
  2. 左侧菜单选择 Networks → Tunnels
  3. 点击 Create a tunnel
  4. 选择 Cloudflared 类型,点击 Next
  5. 给隧道起个名字,比如 my-home-pc,点击 Save tunnel

步骤 2:在 Windows 上安装 Connector

页面会显示安装命令,选择 Windows 标签:

方法 A:直接下载安装(推荐)

  1. 下载 Windows 版 cloudflared:
    👉 https://github.com/cloudflare/cloudflared/releases/latest

    下载 cloudflared-windows-amd64.msi

  2. 双击安装 MSI 文件

  3. 打开 PowerShell(管理员),复制网页上显示的命令运行:

cloudflared.exe service install <你的TOKEN>

⚠️ 这个 Token 是网页上自动生成的一长串字符串,直接复制粘贴即可。

安装完成后,cloudflared 会自动注册为 Windows 服务,开机自启。

步骤 3:配置公共主机名(路由规则)

回到网页面板,点击 Next 进入路由配置:

字段示例值说明
Subdomainweb子域名
Domainexample.com选择你的域名
TypeHTTP服务类型
URLlocalhost:8080本地服务地址

点击 Save tunnel 完成!

常见映射示例

用途TypeURL
本地网站HTTPlocalhost:8080
NAS 管理页面HTTP192.168.1.100:5000
Windows 远程桌面RDPlocalhost:3389
SSH 服务SSHlocalhost:22
Jellyfin 影音HTTPlocalhost:8096
Home AssistantHTTPlocalhost:8123

💡 你可以添加多条规则,一个隧道映射多个子域名到不同的本地服务。


方式二:通过命令行手动配置

步骤 1:下载安装

# 用 winget 安装
winget install cloudflare.cloudflared

# 或手动下载 MSI 安装
# https://github.com/cloudflare/cloudflared/releases/latest

步骤 2:登录认证

cloudflared tunnel login

会自动打开浏览器,选择你的域名授权。

步骤 3:创建隧道

cloudflared tunnel create my-tunnel

记下输出的 Tunnel ID(类似 a]b1c2d3-xxxx-xxxx-xxxx-xxxxxxxxxxxx)。

步骤 4:创建配置文件

C:\Users\<你的用户名>\.cloudflared\ 目录下创建 config.yml

tunnel: <你的隧道ID>
credentials-file: C:\Users\<你的用户名>\.cloudflared\<隧道ID>.json

ingress:
  # 网站服务
  - hostname: web.example.com
    service: http://localhost:8080

  # NAS
  - hostname: nas.example.com
    service: http://192.168.1.100:5000

  # 兜底规则(必须有)
  - service: http_status:404

步骤 5:添加 DNS 记录

cloudflared tunnel route dns my-tunnel web.example.com
cloudflared tunnel route dns my-tunnel nas.example.com

步骤 6:测试运行

cloudflared tunnel run my-tunnel

步骤 7:注册为 Windows 服务(开机自启)

# 以管理员身份运行 PowerShell
cloudflared service install

之后可以在 Windows 服务管理器services.msc)中看到 cloudflared 服务。

# 服务管理命令
net start cloudflared    # 启动
net stop cloudflared     # 停止

三、验证是否成功

1. 检查隧道状态

cloudflared tunnel info my-tunnel

2. 在 Zero Trust 面板查看

进入 Networks → Tunnels,隧道状态应显示为 🟢 HEALTHY

3. 浏览器访问

直接访问 https://web.example.com,应该能看到你本地的服务。


四、常见问题排查

❌ 隧道显示 INACTIVE

# 检查服务是否运行
Get-Service cloudflared

# 查看日志
cloudflared tunnel run my-tunnel  # 前台运行看报错

❌ 502 Bad Gateway

  • 本地服务没有启动
  • 端口号写错了
  • 本地服务绑定了 127.0.0.1 而你写了其他 IP

❌ 防火墙问题

# cloudflared 只需要出站连接,不需要开放入站端口
# 但确保 Windows 防火墙没有阻止 cloudflared.exe 的出站连接

❌ 想要卸载

# 管理员 PowerShell
cloudflared service uninstall
winget uninstall cloudflare.cloudflared

五、实用技巧

🔒 添加访问控制(防止公开暴露)

在 Zero Trust 面板 → Access → Applications 中,可以为你的域名添加认证:

  • 邮箱验证码登录
  • GitHub / Google 登录
  • IP 白名单
这样即使别人知道你的域名,也无法直接访问你的内网服务,非常安全!

📊 架构示意

互联网用户
    │
    ▼
Cloudflare CDN (HTTPS + DDoS防护)
    │
    ▼ (加密隧道,仅出站连接)
你的 Windows 电脑 (cloudflared 服务)
    │
    ▼
本地服务 (网站/NAS/远程桌面等)
0

评论区