ByteNoteByteNote

字节笔记本

2026年2月20日

Cloudflare Tunnel 反向代理配置完全指南

本文介绍 Cloudflare Tunnel 的配置方法,帮助你无需公网 IP 和端口转发,安全地将本地服务暴露到互联网。

什么是 Cloudflare Tunnel

Cloudflare Tunnel 是 Cloudflare 提供的一项免费服务,通过 cloudflared 客户端在你的本地网络和 Cloudflare 全球网络之间建立加密隧道。相比传统的反向代理方案,它具有以下优势:

  • 无需公网 IP - 不需要向运营商申请公网 IP 地址
  • 无需端口转发 - 不需要在路由器上配置端口映射
  • 自动 HTTPS - 自动为域名配置 SSL 证书
  • DDoS 防护 - 享受 Cloudflare 的全球 DDoS 防护
  • 零信任安全 - 支持身份验证和访问控制

适用场景

  • 远程访问家中的 NAS、Home Assistant 等服务
  • 将本地开发环境临时暴露给团队成员
  • 为没有公网 IP 的服务器提供外部访问
  • 替代传统的 frp、ngrok 等内网穿透工具

前置要求

  • 一个 Cloudflare 账号
  • 一个已添加到 Cloudflare 的域名
  • 本地需要暴露的服务(如运行在 localhost:3000)

安装 cloudflared

macOS 安装

bash
# 使用 Homebrew 安装
brew install cloudflared

# 验证安装
cloudflared --version

Linux 安装

bash
# 下载安装脚本
curl -L --output cloudflared.deb https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb

# 安装(Debian/Ubuntu)
sudo dpkg -i cloudflared.deb

# 验证安装
cloudflared --version

Windows 安装

  1. GitHub Releases 下载 Windows 版本
  2. 解压并将 cloudflared.exe 添加到系统 PATH

配置步骤

第一步:登录 Cloudflare

bash
cloudflared tunnel login

运行后会打开浏览器,选择要授权的域名,授权后会在本地生成证书文件(~/.cloudflared/cert.pem)。

第二步:创建隧道

bash
# 创建隧道(替换 my-tunnel 为你喜欢的名称)
cloudflared tunnel create my-tunnel

# 输出示例:
# Tunnel credentials written to /Users/username/.cloudflared/<tunnel-id>.json
# cloudflared tunnel route-dns my-tunnel <tunnel-id>.cfargotunnel.com

创建成功后会显示隧道的 UUID,并生成凭证文件。

第三步:配置 config.yaml

创建配置文件 ~/.cloudflared/config.yaml

yaml
tunnel: <你的隧道 UUID>
credentials-file: /Users/username/.cloudflared/<tunnel-id>.json

ingress:
  # 子域名 1:映射到本地服务
  - hostname: app.yourdomain.com
    service: http://localhost:3000

  # 子域名 2:映射到另一个服务
  - hostname: api.yourdomain.com
    service: http://localhost:8080

  # 子域名 3:映射到 Home Assistant
  - hostname: home.yourdomain.com
    service: http://localhost:8123

  # 默认规则:拒绝其他请求
  - service: http_status:404

配置说明:

配置项说明
tunnel隧道 UUID,创建时自动生成
credentials-file凭证文件路径
ingress路由规则列表,按顺序匹配
hostname外部访问的子域名
service本地服务地址

第四步:配置 DNS 记录

将子域名指向隧道:

bash
# 为每个子域名创建 CNAME 记录
cloudflared tunnel route dns my-tunnel app.yourdomain.com
cloudflared tunnel route dns my-tunnel api.yourdomain.com
cloudflared tunnel route dns my-tunnel home.yourdomain.com

或者手动在 Cloudflare 控制台添加 CNAME 记录:

  • 名称:app
  • 目标:<tunnel-id>.cfargotunnel.com

第五步:启动隧道

前台运行(测试):

bash
cloudflared tunnel run my-tunnel

看到 Connected 字样表示连接成功。

后台运行(生产):

macOS 使用 LaunchAgent

创建启动配置文件 ~/Library/LaunchAgents/com.cloudflare.cloudflared.plist

xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.cloudflare.cloudflared</string>
    <key>ProgramArguments</key>
    <array>
        <string>/opt/homebrew/bin/cloudflared</string>
        <string>tunnel</string>
        <string>run</string>
        <string>my-tunnel</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/tmp/cloudflared.out</string>
    <key>StandardErrorPath</key>
    <string>/tmp/cloudflared.err</string>
</dict>
</plist>

加载并启动服务:

bash
launchctl load ~/Library/LaunchAgents/com.cloudflare.cloudflared.plist
launchctl start com.cloudflare.cloudflared

Linux 使用 systemd

创建服务文件 /etc/systemd/system/cloudflared.service

ini
[Unit]
Description=Cloudflare Tunnel
After=network.target

[Service]
Type=simple
User=root
ExecStart=/usr/bin/cloudflared tunnel run my-tunnel
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

启动服务:

bash
sudo systemctl enable cloudflared
sudo systemctl start cloudflared
sudo systemctl status cloudflared

高级配置

启用零信任身份验证

在 Cloudflare Zero Trust 控制台中:

  1. 进入 AccessApplications
  2. 点击 Add an application
  3. 选择 Self-hosted
  4. 配置应用名称和域名
  5. 添加身份验证策略(如 Google、GitHub OAuth)

配置多个隧道

可以创建多个隧道分别管理不同服务:

bash
# 创建开发环境隧道
cloudflared tunnel create dev-tunnel

# 创建生产环境隧道
cloudflared tunnel create prod-tunnel

使用负载均衡

多个 cloudflared 实例可以连接到同一个隧道,实现高可用:

bash
# 在服务器 A 上运行
cloudflared tunnel run my-tunnel

# 在服务器 B 上运行(相同配置)
cloudflared tunnel run my-tunnel

常用命令

bash
# 列出所有隧道
cloudflared tunnel list

# 查看隧道信息
cloudflared tunnel info my-tunnel

# 删除隧道
cloudflared tunnel delete my-tunnel

# 查看日志
cloudflared tunnel tail my-tunnel

# 验证配置
cloudflared tunnel ingress validate

# 测试路由规则
cloudflared tunnel ingress rule https://app.yourdomain.com

故障排查

隧道无法连接

bash
# 检查凭证文件是否存在
ls -la ~/.cloudflared/

# 查看详细日志
cloudflared tunnel run my-tunnel --log-level debug

DNS 解析问题

确保 CNAME 记录指向 <tunnel-id>.cfargotunnel.com,而不是直接指向 IP。

本地服务不可访问

bash
# 测试本地服务是否正常
curl http://localhost:3000

# 检查防火墙设置
sudo lsof -i :3000

与 Tailscale 对比

特性Cloudflare TunnelTailscale
网络类型基于云的中继P2P + DERP
公网 IP不需要不需要
域名支持原生支持需配合其他服务
身份验证Cloudflare AccessTailscale Auth
适用场景公开服务私有网络

两者可以结合使用:Tailscale 用于私有网络访问,Cloudflare Tunnel 用于公开特定服务。

总结

Cloudflare Tunnel 是一个强大且免费的内网穿透解决方案,特别适合:

  • 没有公网 IP 的家庭网络
  • 需要快速暴露本地服务的开发场景
  • 希望简化运维的中小型项目

通过简单的配置,你就可以安全地将本地服务暴露到互联网,享受 Cloudflare 的全球加速和安全防护。

参考链接

分享: