Skip to content

Instantly share code, notes, and snippets.

@xiaozhouzhoua
Created January 31, 2026 14:41
Show Gist options
  • Select an option

  • Save xiaozhouzhoua/0fe661ac5ea5d8a7c30a487a8e040f00 to your computer and use it in GitHub Desktop.

Select an option

Save xiaozhouzhoua/0fe661ac5ea5d8a7c30a487a8e040f00 to your computer and use it in GitHub Desktop.
Cloudflare Tunnel 使用指南

Cloudflare Tunnel 使用指南

简介

Cloudflare Tunnel(cloudflared)可以将本地服务安全地暴露到公网,无需公网 IP、无需开放端口。

安装

brew install cloudflared

首次配置

1. 登录 Cloudflare

cloudflared tunnel login

浏览器会自动打开,选择要使用的域名并授权。

2. 创建隧道

cloudflared tunnel create <隧道名称>

例如:cloudflared tunnel create service

3. 配置 DNS

cloudflared tunnel route dns <隧道名称> <子域名>

例如:cloudflared tunnel route dns service test.example.com

这会自动在 Cloudflare DNS 添加 CNAME 记录。

4. 获取 Token 并创建凭证文件

# 查看隧道列表
cloudflared tunnel list

# 获取 token
cloudflared tunnel token <隧道名称>

将 token 解码后创建凭证文件 ~/.cloudflared/<隧道ID>.json

{
  "AccountTag": "<账户ID>",
  "TunnelSecret": "<隧道密钥>",
  "TunnelID": "<隧道ID>"
}

5. 创建配置文件

创建 ~/.cloudflared/config.yml

tunnel: <隧道ID>
credentials-file: ~/.cloudflared/<隧道ID>.json

日常使用

启动隧道

cloudflared tunnel run <隧道名称>

临时暴露服务(无需配置)

cloudflared tunnel --url http://localhost:8080

会生成一个临时的 *.trycloudflare.com 域名。

管理界面

Tunnel 管理入口在 Cloudflare Zero Trust:

  1. 访问 https://one.dash.cloudflare.com
  2. 进入 NetworksTunnels

在这里可以:

  • 查看隧道状态
  • 修改 ingress 配置(端口映射)
  • 添加更多域名路由

常用命令

# 查看隧道列表
cloudflared tunnel list

# 查看隧道详情
cloudflared tunnel info <隧道名称>

# 删除隧道
cloudflared tunnel delete <隧道名称>

# 查看版本
cloudflared --version

设为系统服务(可选)

sudo cloudflared service install
sudo launchctl start com.cloudflare.cloudflared

相关工具对比

工具 用途 需要公网 IP
Cloudflare Tunnel 内网穿透,暴露服务到公网
Tailscale 组建私有 mesh VPN
Caddy Web 服务器 / 反向代理

最后更新:2026-01-31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment