Cloudflare Tunnel
Create secure tunnels to expose local services through Cloudflare's network without opening inbound firewall ports. Supports HTTP/HTTPS services, TCP tunnels, and zero-trust access controls.
Prerequisites
- - Cloudflare account with a domain
- INLINECODE0 CLI installed
- Domain DNS managed by Cloudflare
- Local services running (web servers, APIs, etc.)
Installation
macOS (Homebrew)
CODEBLOCK0
Linux
CODEBLOCK1
Windows
CODEBLOCK2
Usage
Authentication
Login to Cloudflare:
CODEBLOCK3
Basic Tunnel Setup
Create and run a tunnel:
CODEBLOCK4
Persistent Tunnel Configuration
Create tunnel and configure DNS:
CODEBLOCK5
Configuration file (~/.cloudflared/config.yml):
CODEBLOCK6
Run configured tunnel:
CODEBLOCK7
Advanced Configuration
Multiple services configuration:
CODEBLOCK8
Advanced origin request options:
CODEBLOCK9
Service Management
Tunnel management commands:
CODEBLOCK10
DNS management:
CODEBLOCK11
Zero Trust Access Control
Access policy configuration (via Cloudflare Dashboard):
- 1. Go to Cloudflare Zero Trust → Access → Applications
- Add application:
- Application type: Self-hosted
- App domain: admin.yourdomain.com
- Policy name: Admin Access
- 3. Create access policy:
- Allow/Block/Bypass
- Include: Email domain contains @yourcompany.com
- Require: Country is in Taiwan
Service authentication token:
CODEBLOCK12
System Service Setup
Linux systemd service:
CODEBLOCK13
CODEBLOCK14
macOS LaunchAgent:
CODEBLOCK15
CODEBLOCK16
Monitoring and Troubleshooting
Health checking:
CODEBLOCK17
Common troubleshooting:
CODEBLOCK18
Production Example Setup
Complete production deployment:
CODEBLOCK19
Backup and Migration
Backup tunnel configuration:
CODEBLOCK20
Migration to new server:
CODEBLOCK21
This skill enables secure, firewall-friendly exposure of local services through Cloudflare's global network with built-in DDoS protection and zero-trust access controls.
Cloudflare 隧道
通过 Cloudflare 网络创建安全隧道以暴露本地服务,无需开放入站防火墙端口。支持 HTTP/HTTPS 服务、TCP 隧道和零信任访问控制。
前提条件
- - 拥有域名的 Cloudflare 账户
- 已安装 cloudflared 命令行工具
- 域名 DNS 由 Cloudflare 管理
- 本地服务正在运行(Web 服务器、API 等)
安装
macOS(Homebrew)
bash
brew install cloudflare/cloudflare/cloudflared
Linux
bash
下载最新版本
wget https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64
chmod +x cloudflared-linux-amd64
sudo mv cloudflared-linux-amd64 /usr/local/bin/cloudflared
Windows
powershell
使用 winget
winget install --id Cloudflare.cloudflared
或从 GitHub 发布版下载
使用方法
身份验证
登录 Cloudflare:
bash
登录(打开浏览器进行 OAuth 认证)
cloudflared tunnel login
验证身份验证
cloudflared tunnel list
基本隧道设置
创建并运行隧道:
bash
创建命名隧道
cloudflared tunnel create mytunnel
为 HTTP 服务运行隧道
cloudflared tunnel --url http://localhost:3000
使用自定义主机名运行隧道
cloudflared tunnel --url http://localhost:3000 --hostname app.yourdomain.com
快速临时隧道(随机子域名)
cloudflared tunnel --url http://localhost:8080
持久隧道配置
创建隧道并配置 DNS:
bash
创建隧道
cloudflared tunnel create production-app
注意输出中的隧道 ID
创建 DNS 记录
cloudflared tunnel route dns production-app app.yourdomain.com
创建配置文件
mkdir -p ~/.cloudflared
配置文件(~/.cloudflared/config.yml):
yaml
tunnel: production-app
credentials-file: /Users/username/.cloudflared/TUNNEL_ID.json
ingress:
# 主应用
- hostname: app.yourdomain.com
service: http://localhost:3000
# API 服务
- hostname: api.yourdomain.com
service: http://localhost:4000
# 静态文件
- hostname: static.yourdomain.com
service: http://localhost:8080
# WebSocket 服务
- hostname: ws.yourdomain.com
service: ws://localhost:5000
# SSH 访问(需要 Cloudflare for Teams)
- hostname: ssh.yourdomain.com
service: ssh://localhost:22
# 默认规则(必需)
- service: http_status:404
运行配置好的隧道:
bash
使用配置文件运行
cloudflared tunnel run production-app
后台运行
cloudflared tunnel run production-app &
检查隧道状态
cloudflared tunnel info production-app
高级配置
多服务配置:
yaml
tunnel: multi-service-tunnel
credentials-file: /Users/username/.cloudflared/TUNNEL_ID.json
ingress:
# 主网站
- hostname: yourdomain.com
service: http://localhost:3000
# 带身份验证的管理面板
- hostname: admin.yourdomain.com
service: http://localhost:3001
originRequest:
noTLSVerify: true
# 开发 API
- hostname: dev-api.yourdomain.com
service: http://localhost:4000
originRequest:
httpHostHeader: localhost:4000
# 多实例负载均衡
- hostname: lb.yourdomain.com
service: http://localhost:3000
originRequest:
bastionMode: true
# 带自定义头部的文件服务器
- hostname: files.yourdomain.com
service: http://localhost:8000
originRequest:
httpHostHeader: files.local
originServerName: files.local
# 默认兜底规则
- service: http_status:404
高级源请求选项:
yaml
originRequest:
# 禁用 TLS 验证(用于自签名证书)
noTLSVerify: true
# 自定义 HTTP 头部
httpHostHeader: internal.service.local
# 连接超时
connectTimeout: 30s
# 保持连接设置
keepAliveConnections: 100
keepAliveTimeout: 90s
# 代理设置
proxyAddress: http://proxy:8080
proxyPort: 8080
# 堡垒模式(用于 kubectl/ssh)
bastionMode: true
服务管理
隧道管理命令:
bash
列出所有隧道
cloudflared tunnel list
获取隧道信息
cloudflared tunnel info TUNNEL_NAME
删除隧道
cloudflared tunnel delete TUNNEL_NAME
清理未使用的隧道
cloudflared tunnel cleanup TUNNEL_NAME
更新隧道
cloudflared tunnel route dns TUNNEL_NAME new-subdomain.yourdomain.com
DNS 管理:
bash
添加 DNS 路由
cloudflared tunnel route dns mytunnel app.yourdomain.com
列出 DNS 路由
cloudflared tunnel route list
删除 DNS 路由
cloudflared tunnel route delete ROUTE_ID
零信任访问控制
访问策略配置(通过 Cloudflare 仪表板):
- 1. 进入 Cloudflare Zero Trust → Access → Applications
- 添加应用:
- 应用类型:Self-hosted
- 应用域名:admin.yourdomain.com
- 策略名称:Admin Access
- 3. 创建访问策略:
- 允许/阻止/绕过
- 包含:邮箱域名包含 @yourcompany.com
- 要求:国家为台湾
服务身份验证令牌:
bash
为 API 访问创建服务令牌
(通过 Cloudflare 仪表板 → Zero Trust → Access → Service Tokens 完成)
在请求中使用服务令牌
curl -H CF-Access-Client-Id: TOKEN_ID \
-H CF-Access-Client-Secret: TOKEN_SECRET \
https://api.yourdomain.com/data
系统服务设置
Linux systemd 服务:
ini
/etc/systemd/system/cloudflared-tunnel.service
[Unit]
Description=Cloudflare Tunnel
After=network.target
[Service]
Type=simple
User=cloudflared
ExecStart=/usr/local/bin/cloudflared tunnel run production-app
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
bash
启用并启动服务
sudo systemctl enable cloudflared-tunnel
sudo systemctl start cloudflared-tunnel
sudo systemctl status cloudflared-tunnel
macOS LaunchAgent:
xml
Label
com.cloudflare.tunnel
ProgramArguments
/usr/local/bin/cloudflared
tunnel
run
production-app
RunAtLoad
KeepAlive
bash
加载并启动 LaunchAgent
launchctl load ~/Library/LaunchAgents/com.cloudflare.tunnel.plist
launchctl start com.cloudflare.tunnel
监控与故障排除
健康检查:
bash
检查隧道连接
curl -H Host: yourdomain.com http://localhost:3000
测试外部访问
curl https://yourdomain.com
检查隧道日志
cloudflared tunnel --loglevel debug run production-app
监控指标(如果启用)
curl http://localhost:2000/metrics
常见故障排除:
bash
检查隧道状态
cloudflared tunnel info production-app
验证配置文件
cloudflared tunnel ingress