概述
使用 Cloudflare Tunnel 将本地开发端口安全地暴露到公网,支持固定域名映射,无需配置防火墙或路由器端口转发。
优势
- 固定 URL: 重启后 URL 不变
- 多端口支持: 一个 tunnel 支持多个端口
- 自动 SSL: Cloudflare 自动处理 HTTPS 证书
- 安全: 不暴露服务器 IP,通过 Cloudflare CDN 访问
- 零配置: 无需修改路由器或防火墙
前置条件
-
安装 cloudflared
bash1brew install cloudflare/cloudflare/cloudflared -
拥有 Cloudflare 托管的域名
一次性配置步骤
Step 1: 登录 Cloudflare
bash
1
cloudflared tunnel login
这会打开浏览器,选择要使用的域名并授权。授权后会在 ~/.cloudflared/ 目录生成 cert.pem 文件。
Step 2: 创建 Tunnel
bash
12
# 创建一个命名的 tunnel
cloudflared tunnel create my-dev-tunnel
输出示例:
plaintext
12
Tunnel credentials written to /Users/your-username/.cloudflared/f6892ee7-xxxx-xxxx-xxxx-xxxxxxxxxxxx.json
Created tunnel my-dev-tunnel with id f6892ee7-xxxx-xxxx-xxxx-xxxxxxxxxxxx
重要: 记下 Tunnel ID,后续配置需要用到。
Step 3: 创建配置文件
创建 ~/.cloudflared/config.yml:
yaml
1234567891011121314151617181920212223242526
# Tunnel ID (替换为你的实际 ID)
tunnel: f6892ee7-xxxx-xxxx-xxxx-xxxxxxxxxxxx
# 凭证文件路径 (替换为你的实际路径)
credentials-file: /Users/your-username/.cloudflared/f6892ee7-xxxx-xxxx-xxxx-xxxxxxxxxxxx.json
# 端口映射规则
ingress:
# 3000 端口 - Next.js/通用 Web 服务
- hostname: p3000.your-domain.com
service: http://localhost:3000
# 5173 端口 - Vite 开发服务器
- hostname: p5173.your-domain.com
service: http://localhost:5173
# 4321 端口 - Astro 开发服务器
- hostname: p4321.your-domain.com
service: http://localhost:4321
# 8787 端口 - Cloudflare Workers 本地开发
- hostname: p8787.your-domain.com
service: http://localhost:8787
# 默认规则 (必须存在,放在最后)
- service: http_status:404
配置说明:
-
tunnel: 你的 Tunnel ID -
credentials-file: 凭证文件的绝对路径 -
ingress: 端口映射规则,按顺序匹配 - 最后一条规则必须是默认规则 (无 hostname)
Step 4: 配置 DNS 路由
为每个端口创建 DNS 记录:
bash
1234567891011
# 3000 端口
cloudflared tunnel route dns my-dev-tunnel p3000.your-domain.com
# 5173 端口
cloudflared tunnel route dns my-dev-tunnel p5173.your-domain.com
# 4321 端口
cloudflared tunnel route dns my-dev-tunnel p4321.your-domain.com
# 8787 端口
cloudflared tunnel route dns my-dev-tunnel p8787.your-domain.com
成功输出:
plaintext
1
Added CNAME p3000.your-domain.com which will route to this tunnel
这会在 Cloudflare DNS 中自动创建 CNAME 记录,指向 tunnel。
日常使用
启动 Tunnel
bash
12345678
# 前台运行 (查看日志)
cloudflared tunnel run my-dev-tunnel
# 后台运行
cloudflared tunnel run my-dev-tunnel &
# 使用 nohup 后台运行 (退出终端后继续运行)
nohup cloudflared tunnel run my-dev-tunnel > /dev/null 2>&1 &
停止 Tunnel
bash
12345678
# 查找进程
ps aux | grep cloudflared
# 杀死进程
kill <进程ID>
# 或者使用 pkill
pkill cloudflared
查看 Tunnel 状态
bash
12345
# 列出所有 tunnel
cloudflared tunnel list
# 查看特定 tunnel 信息
cloudflared tunnel info my-dev-tunnel
开机自启动 (可选)
macOS
bash
1234567891011
# 安装为系统服务
cloudflared service install
# 启动服务
sudo launchctl start com.cloudflare.cloudflared
# 停止服务
sudo launchctl stop com.cloudflare.cloudflared
# 卸载服务
cloudflared service uninstall
Linux (systemd)
bash
1234567891011
# 安装服务
sudo cloudflared service install
# 启动服务
sudo systemctl start cloudflared
# 开机自启
sudo systemctl enable cloudflared
# 查看状态
sudo systemctl status cloudflared
端口映射列表
| 端口 | 域名 | 用途 |
|---|---|---|
| 3000 | https://p3000.your-domain.com | Next.js / 通用 Web 服务 |
| 5173 | https://p5173.your-domain.com | Vite 开发服务器 |
| 4321 | https://p4321.your-domain.com | Astro 开发服务器 |
| 8787 | https://p8787.your-domain.com | Cloudflare Workers 本地开发 |
注意: 只有对应端口有服务运行时,访问域名才会有响应。
常见问题
1. 修改配置后如何生效?
修改 config.yml 后,需要重启 tunnel:
bash
12345
# 停止现有 tunnel
pkill cloudflared
# 重新启动
cloudflared tunnel run my-dev-tunnel
2. 如何添加新端口?
步骤:
- 编辑
~/.cloudflared/config.yml,在ingress中添加新规则 - 配置 DNS:
cloudflared tunnel route dns my-dev-tunnel p新端口.your-domain.com - 重启 tunnel
示例 - 添加 8080 端口:
yaml
12345
ingress:
# ... 现有规则
- hostname: p8080.your-domain.com
service: http://localhost:8080
- service: http_status:404
bash
12
cloudflared tunnel route dns my-dev-tunnel p8080.your-domain.com
pkill cloudflared && cloudflared tunnel run my-dev-tunnel
3. 如何删除端口映射?
删除 DNS 记录:
- 登录 Cloudflare Dashboard
- 选择域名 → DNS → Records
- 找到对应的 CNAME 记录并删除
- 从
config.yml中删除对应规则 - 重启 tunnel
4. 如何查看 tunnel 日志?
前台运行查看实时日志:
bash
1
cloudflared tunnel run my-dev-tunnel
系统服务日志 (macOS):
bash
1
log show --predicate 'process == "cloudflared"' --last 1h
系统服务日志 (Linux):
bash
1
sudo journalctl -u cloudflared -f
5. Tunnel 连接失败怎么办?
检查步骤:
- 确认 tunnel 正在运行:
cloudflared tunnel list - 检查本地服务是否启动:
lsof -i:3000 - 检查 config.yml 配置是否正确
- 检查 DNS 是否生效:
nslookup p3000.your-domain.com - 查看 tunnel 日志排查错误
6. 如何使用不同的 Tunnel?
如果有多个 tunnel,在运行时指定名称:
bash
1
cloudflared tunnel run <tunnel-name>
或在 config.yml 中指定不同的 tunnel ID。
安全建议
- 保护凭证文件:
~/.cloudflared/*.json文件包含敏感信息,不要提交到 Git 仓库 - 设置文件权限:
bash12
chmod 600 ~/.cloudflared/*.json chmod 600 ~/.cloudflared/config.yml - 使用环境变量: 敏感配置可以通过环境变量传递
- 定期更新 cloudflared:
brew upgrade cloudflared - 监控访问日志: 定期检查 Cloudflare Analytics
文件位置
plaintext
1234
~/.cloudflared/
├── cert.pem # Cloudflare 认证证书
├── config.yml # Tunnel 配置文件
└── <tunnel-id>.json # Tunnel 凭证文件 (敏感)
不要将这些文件提交到 Git 仓库!
建议在项目的 .gitignore 中添加:
plaintext
12
.cloudflared/
*.json