Cloudflare Tunnel 内网穿透
一句话概述:Cloudflare Tunnel 把你家里/办公室的服务安全地暴露到公网,不用开端口、不用公网 IP、不用配证书,你的机器主动连 Cloudflare,流量走加密隧道,还自带 DDoS 防护和 WAF,完全免费。
核心知识点
| 概念 | 白话解释 |
|---|
| 内网穿透 | 让外网能访问你内网的服务(比如家里的 NAS) |
| cloudflared | 隧道客户端,装在你的机器上,主动连接 Cloudflare |
| 出站连接 | 你的机器主动连 Cloudflare,不需要开入站端口 |
| Zero Trust | 零信任安全模型,可以加 SSO 登录才能访问 |
| QUIC | 2026 默认使用 HTTP/3 QUIC 协议,更快更稳 |
| Ingress Rule | 入口规则,定义哪个域名转发到哪个本地服务 |
安装配置
安装 cloudflared
# Linux (Debian/Ubuntu)
curl -L --output cloudflared.deb https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
sudo dpkg -i cloudflared.deb # 安装
# macOS
brew install cloudflared
# Docker
docker run -d cloudflare/cloudflared:latest tunnel --no-autoupdate run --token <TOKEN>
创建隧道(推荐:面板方式)
1. 登录 https://one.dash.cloudflare.com
2. Networks → Tunnels → Create a tunnel
3. 选择 Cloudflared → 命名隧道
4. 复制安装命令(包含 Token)
5. 在你的机器上运行安装命令
6. 配置 Public Hostname:
- 子域名:app.yourdomain.com
- Service:http://localhost:3000(你本地服务的地址)
7. 保存 → 隧道自动建立
命令行方式
# 登录
cloudflared tunnel login # 浏览器授权
# 创建隧道
cloudflared tunnel create my-tunnel # 创建名为 my-tunnel 的隧道
# 配置 DNS
cloudflared tunnel route dns my-tunnel app.yourdomain.com # 创建 DNS 记录
# 运行隧道
cloudflared tunnel run my-tunnel # 启动隧道
本地配置文件方式
# ~/.cloudflared/config.yml
tunnel: <隧道UUID> # 隧道 ID
credentials-file: /home/user/.cloudflared/<隧道UUID>.json # 凭证文件
ingress: # 入口规则
- hostname: app.yourdomain.com # 域名
service: http://localhost:3000 # 转发到本地 3000 端口
- hostname: api.yourdomain.com
service: http://localhost:8080 # 另一个服务
- hostname: ssh.yourdomain.com
service: ssh://localhost:22 # SSH 访问
- service: http_status:404 # 默认返回 404(必须有这条兜底)
# 安装为系统服务(开机自启)
sudo cloudflared service install # 注册为 systemd 服务
sudo systemctl start cloudflared # 启动
sudo systemctl enable cloudflared # 开机自启
基本使用
快速暴露本地服务(临时)
# 一行命令临时暴露(不需要域名,生成随机 URL)
cloudflared tunnel --url http://localhost:3000 # 生成 xxx.trycloudflare.com 链接
# 适合临时演示、调试 Webhook
暴露多个服务
# config.yml
ingress:
- hostname: web.example.com
service: http://localhost:3000 # Web 前端
- hostname: api.example.com
service: http://localhost:8080 # API 后端
- hostname: db.example.com
service: tcp://localhost:5432 # PostgreSQL(TCP 协议)
- service: http_status:404
高级用法
添加 Zero Trust 访问控制
在 Cloudflare One 面板:
Access → Applications → Add Application
1. 选择 Self-hosted
2. 输入域名:admin.example.com
3. 添加策略:
- 只允许特定邮箱域名(如 @company.com)
- 需要 Google/GitHub SSO 登录
- 加一次性密码(OTP)验证
SSH 浏览器访问
# config.yml
ingress:
- hostname: ssh.example.com
service: ssh://localhost:22 # 暴露 SSH
在 Access → Applications 配置后
可以直接在浏览器中 SSH,不需要 SSH 客户端
访问 https://ssh.example.com 登录后进入终端
常见报错
| 报错信息 | 原因 | 解决方案 |
|---|
Connection refused | 本地服务没启动 | 确保本地服务在运行 |
Unable to reach origin | service 地址写错 | 检查 config.yml 中的 service URL |
DNS record not found | DNS 没配置 | cloudflared tunnel route dns 或面板配置 |
Bad gateway (502) | 后端服务出错 | 检查本地服务的日志 |
Tunnel is unhealthy | cloudflared 断线 | 检查网络连接和 cloudflared 日志 |
| 证书问题 | 本地用了自签证书 | 在 ingress 中加 noTLSVerify: true |
速查表
# cloudflared 命令
cloudflared tunnel login # 登录
cloudflared tunnel create <名称> # 创建隧道
cloudflared tunnel list # 列出隧道
cloudflared tunnel run <名称> # 运行隧道
cloudflared tunnel delete <名称> # 删除隧道
cloudflared tunnel route dns <名> <域名> # 配置 DNS
cloudflared tunnel info <名称> # 隧道信息
cloudflared tunnel --url http://localhost:3000 # 快速临时隧道
cloudflared service install # 安装为系统服务
cloudflared update # 更新 cloudflared
# 支持的协议
http:// / https:// # HTTP/HTTPS
tcp:// # TCP(数据库等)
ssh:// # SSH
rdp:// # 远程桌面
unix:///path/sock # Unix Socket
参考:Cloudflare Tunnel 文档 | Zero Trust