Featured image of post Cloudflare Tunnels 完全安装与内网穿透配置指南

Cloudflare Tunnels 完全安装与内网穿透配置指南

Cloudflare Tunnels 完全安装与内网穿透配置指南

Cloudflare Tunnel 是 Cloudflare 提供的零信任网络访问方案,通过在本地运行一个轻量级 daemon(cloudflared),在内网服务和 Cloudflare 边缘之间建立加密隧道。无需公网 IP、无需开放端口、无需配置 NAT 或防火墙规则,就能把内网的 Web 服务、SSH、数据库等安全暴露出去。

本文将手把手教你从安装到配置的完整流程,覆盖 Linux、macOS、Docker 以及 Windows 平台,并分别演示 Web HTTP、SSH、TCP 服务穿透的实际操作。

一、安装 cloudflared

Debian / Ubuntu

1
2
3
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64 -o /usr/local/bin/cloudflared
chmod +x /usr/local/bin/cloudflared
cloudflared --version

ARM 架构机器(如树莓派):

1
2
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-arm64 -o /usr/local/bin/cloudflared
chmod +x /usr/local/bin/cloudflared

CentOS / RHEL / Fedora

1
2
3
4
5
6
7
8
cat <<'EOF' > /etc/yum.repos.d/cloudflared.repo
[cloudflared]
name=Cloudflared
baseurl=https://packages.cloudflare.com/cloudflared/rpm
enabled=1
gpgcheck=0
EOF
yum install cloudflared -y

macOS (Homebrew)

1
2
brew install cloudflared
cloudflared --version

Windows

从 GitHub Releases 页面下载最新 .msi 安装包,双击运行安装即可。安装完成后在命令行输入 cloudflared --version 验证。

Docker 方式

1
2
docker run -d --name cloudflared --restart always \
  cloudflare/cloudflared:latest tunnel --no-autoupdate run --token <你的Token>

二、三种 Tunnel 创建方式对比

方式适用场景凭证管理推荐度
Token 模式简单场景,单个服务Token 明文写在命令行或文件里⭐⭐⭐
Config 模式多服务、生产环境YAML 配置文件 + credentials.json⭐⭐⭐⭐⭐
Connectors 模式大规模部署、远程管理cloudflared connect 命令连接代理⭐⭐⭐⭐

下面重点讲解 Token 模式(最简单快速)和 Config 模式(最灵活可靠)。

三、方案 A:Token 模式快速上手

适合只暴露一个服务、不想折腾配置文件的场景。

1. 在 Cloudflare Zero Trust 面板创建隧道

  1. 登录 Cloudflare Zero Trust Dashboard
  2. 左侧菜单选择 NetworksTunnels
  3. 点击 Create a tunnel,选择 Cloudflared 作为 connector
  4. 填写隧道名称
  5. 切换到 Public Hostnames 标签页,添加路由规则:
    • Subdomain:子域名,如 app
    • Domain:选择你的主域名
    • Service:选择 HTTP
    • URL:内网服务地址,如 localhost:8080
  6. 点击 Save tunnel
  7. 获取 Token

2. 获取 Token 并直接启动

从 Zero Trust 面板 → Tunnels → 你的隧道 → Configure token 复制 Token,然后直接在终端运行:

1
cloudflared tunnel --no-autoupdate run --token eyJvIjoi...你的Token...abc123

一个命令搞定。多个不同域名的服务就需要 Config 模式了。

四、方案 B:Config 模式(生产环境推荐)

认证登录

先让本机登录到 Cloudflare 账户:

1
cloudflared tunnel login

执行后会弹出浏览器窗口要求授权。授权完成后,~/.cloudflared/ 目录下会生成 cert.pem 文件。

创建隧道

1
cloudflared tunnel create my-web-tunnel

记录输出的 JSON 凭证文件路径和 Tunnel ID。用以下命令查看隧道列表:

1
cloudflared tunnel list

配置路由规则

创建或编辑 ~/.cloudflared/config.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
tunnel: your-tunnel-id-here
credentials-file: /home/user/.cloudflared/your-tunnel-id.json

ingress:
  # 将 app.example.com 转发到本地 8080 端口的 Web 服务
  - hostname: app.example.com
    service: http://localhost:8080
  
  # 将 ssh.example.com 转发到本地 22 端口(SSH)
  - hostname: ssh.example.com
    service: ssh://localhost:22
  
  # catch-all 兜底规则
  - service: http_status:404

最后的 catch-all 规则(http_status:404)是必须的,没有匹配规则时返回 404。

添加 DNS 路由记录:

1
2
cloudflared tunnel route dns my-web-tunnel app.example.com
cloudflared tunnel route dns my-web-tunnel ssh.example.com

启动 Tunnel

1
cloudflared tunnel --config ~/.cloudflared/config.yml run my-web-tunnel

看到输出中有多行 ConnectedHealthy 字样,说明隧道已成功建立。按 Ctrl+C 停止。

后台服务化

systemd 方式

创建 service 文件 /etc/systemd/system/cloudflared-tunnel.service

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
[Unit]
Description=Cloudflared Tunnel
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
ExecStart=/usr/local/bin/cloudflared tunnel --config /home/user/.cloudflared/config.yml run my-web-tunnel
Restart=on-failure
RestartSec=5
User=user
Group=user

[Install]
WantedBy=multi-user.target
1
2
3
4
sudo systemctl daemon-reload
sudo systemctl enable cloudflared-tunnel
sudo systemctl start cloudflared-tunnel
sudo systemctl status cloudflared-tunnel

Docker Compose 方式

创建 docker-compose.yml

1
2
3
4
5
6
7
version: "3"
services:
  cloudflared:
    image: cloudflare/cloudflared:latest
    container_name: cloudflared
    restart: unless-stopped
    command: tunnel --no-autoupdate run --token eyJvIjoi...你的Token...abc123
1
2
docker compose up -d
docker logs -f cloudflared

五、各种服务的穿透配置

HTTP/HTTPS Web 服务

1
2
3
4
5
6
7
8
ingress:
  - hostname: web.example.com
    service: http://localhost:3000
    originRequest:
      noTLSVerify: false
      connectTimeout: 10s
      httpHostHeader: "localhost:3000"
  - service: http_status:404

originRequest 可自定义超时、是否跳过 TLS 验证、HTTP Host Header 等参数。

SSH 穿透

服务端 config.yml

1
2
3
4
ingress:
  - hostname: ssh.myserver.com
    service: ssh://localhost:22
  - service: http_status:404

客户端连接方式1:直接用 cloudflared 命令行连接

1
cloudflared access ssh --hostname ssh.myserver.com

客户端连接方式2:配置 ~/.ssh/config

1
2
3
4
Host my-server
    HostName ssh.myserver.com
    User root
    ProxyCommand /usr/local/bin/cloudflared access ssh --hostname %h

然后直接 ssh my-server 连入。

TCP 非 HTTP 服务穿透

MySQL(3306)、Redis(6379)等不支持 HTTP 的服务:

1
2
3
4
ingress:
  - hostname: db.example.com
    service: tcp://localhost:3306
  - service: http_status:404

WebSocket 支持

WebSocket 天然支持,不需要额外配置。应用跑在 localhost:8001

1
2
3
4
ingress:
  - hostname: ws.example.com
    service: http://localhost:8001
  - service: http_status:404

多服务多域名

同一个隧道可以同时映射多个域名到不同内网服务:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
ingress:
  - hostname: www.example.com
    service: http://localhost:3000
  - hostname: api.example.com
    service: http://localhost:8080
  - hostname: admin.example.com
    service: http://localhost:8081
  - hostname: ssh.example.com
    service: ssh://localhost:22
  - service: http_status:404

六、高级配置与优化

1. 连接健康检查

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
tunnel: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
credentials-file: /home/user/.cloudflared/xxxx.json
metrics: :9000

ingress:
  - hostname: app.example.com
    service: http://localhost:8080
    originRequest:
      connectTimeout: 30s
      keepAliveConnections: 100
      keepAliveTimeout: 90s
      tcpKeepAlive: 30s

2. 访问控制(Zero Trust Access)

在 Zero Trust 面板 → Access → Applications 创建应用,关联隧道域名,设置规则:

  • 允许特定邮箱域名
  • 允许特定群组
  • 启用 MFA 验证

配置后访问 app.example.com 会被跳转到 Cloudflare 登录页面,认证成功才会进入后端。

3. 日志调试

1
2
3
4
5
# 实时日志
journalctl -u cloudflared-tunnel -f

# 测试 ingress 规则
cloudflared tunnel ingress test --url https://app.example.com

4. 常见问题排查

问题可能原因解决方法
Tunnel 连接后很快断开网络不稳定检查出口网络连接,确认 7844/20000 端口未受阻
无法访问ingress 规则错误或服务未启动cloudflared tunnel ingress test 测试,检查目标服务
SSL 证书报错域名 CNAME 未正确绑定确保域名 CNAME 指向 .trycloudflare.com
登录失败cert.pem 权限或过期重新执行 cloudflared tunnel login

七、总结

  • 快速试用:直接用 --token 模式一行命令启动。
  • 生产部署:推荐 Config 模式 + systemd 托管,配合 Cloudflare Access 做身份验证。
  • 批量管理:考虑 Connectors 模式或脚本自动化方案。

Cloudflare Tunnel 最大的优势就是安全——内网服务不出现在公网面上,不需要开任何防火墙规则。所有流量经过 Cloudflare 边缘节点加密传输,配合 Access 可以做到细粒度的访问控制。

不管是个人的博客、内网管理系统、SSH 跳板机,还是各种私有协议服务,Cloudflare Tunnel 都能胜任。


本文由 BOSH 的博客助手 HerMes 整理 🛡️

原文链接:[本地上传]

热爱生活 学无止境
使用 Hugo 构建
主题 StackJimmy 设计