Tunnel 内网穿透
Cloudflare Tunnel 是一个内网穿透服务,让你无需公网 IP 就能将内网服务暴露到互联网。它通过在本地运行 cloudflared 客户端,建立到 Cloudflare 边缘网络的安全隧道。
Tunnel 简介
什么是 Tunnel?
Tunnel(隧道)是一种安全的内网穿透方案,通过在本地运行轻量级守护进程 cloudflared,创建到 Cloudflare 的仅出站连接。
用户 → Cloudflare 边缘网络 → Tunnel → 本地服务
↑ ↓
HTTPS 加密隧道
Tunnel 的优势
无需公网 IP:即使在内网环境也能暴露服务。
安全:所有流量经过 Cloudflare,享受 DDoS 防护和 WAF。
免费:基础功能完全免费。
简单:无需配置路由器和防火墙。
适用场景
- 暴露本地开发环境
- 访问家庭服务器、NAS
- 搭建个人云服务
- 内部应用远程访问
快速开始
方式一:Zero Trust 控制台(推荐)
步骤一:创建隧道
- 登录 Cloudflare 控制台
- 进入 "Zero Trust" → "Networks" → "Tunnels"
- 点击 "Create a tunnel"
- 选择 "Cloudflared"
- 输入隧道名称
- 点击 "Save tunnel"
步骤二:安装 cloudflared
根据系统选择安装方式:
Windows:
# 使用 winget
winget install cloudflare.cloudflared
# 或下载安装包
# https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/
macOS:
brew install cloudflared
Linux:
# Debian/Ubuntu
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -o cloudflared.deb
sudo dpkg -i cloudflared.deb
# CentOS/RHEL
curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.rpm -o cloudflared.rpm
sudo rpm -i cloudflared.rpm
步骤三:连接隧道
在控制台复制安装命令,类似:
cloudflared service install eyJhIjoixxxxxxxxxx
运行后,隧道会自动连接到 Cloudflare。
步骤四:配置服务
- 在隧道设置中点击 "Public Hostname"
- 添加主机名配置:
- Subdomain:
nas - Domain: 选择你的域名
- Service:
http://localhost:5000
- Subdomain:
- 保存配置
现在可以通过 https://nas.example.com 访问本地服务。
方式二:命令行配置
登录认证
cloudflared tunnel login
这会打开浏览器,选择要授权的域名。
创建隧道
cloudflared tunnel create my-tunnel
保存输出的隧道 ID。
创建配置文件
创建 ~/.cloudflared/config.yml:
tunnel: <tunnel-id>
credentials-file: /root/.cloudflared/<tunnel-id>.json
ingress:
- hostname: nas.example.com
service: http://localhost:5000
- hostname: ssh.example.com
service: ssh://localhost:22
- service: http_status:404
配置 DNS
cloudflared tunnel route dns my-tunnel nas.example.com
运行隧道
cloudflared tunnel run my-tunnel
作为服务运行
# 安装为系统服务
cloudflared service install
# 启动服务
sudo systemctl start cloudflared
sudo systemctl enable cloudflared
服务类型配置
HTTP 服务
ingress:
- hostname: web.example.com
service: http://localhost:8080
HTTPS 服务
ingress:
- hostname: secure.example.com
service: https://localhost:8443
originRequest:
noTLSVerify: true
SSH 服务
ingress:
- hostname: ssh.example.com
service: ssh://localhost:22
访问时需要使用 Cloudflare SSH 客户端:
# 安装 SSH 客户端
cloudflared access ssh-config
# SSH 连接
ssh nas.example.com
远程桌面(RDP)
ingress:
- hostname: rdp.example.com
service: rdp://localhost:3389
TCP 任意端口
ingress:
- hostname: tcp.example.com
service: tcp://localhost:3306
Docker 部署
使用 Docker 运行 cloudflared
docker run -d --name cloudflared \
-v ~/.cloudflared:/etc/cloudflared:ro \
cloudflare/cloudflared:latest \
tunnel --config /etc/cloudflared/config.yml run
Docker Compose
version: '3'
services:
cloudflared:
image: cloudflare/cloudflared:latest
command: tunnel --no-autoupdate run --token <token>
restart: unless-stopped
web:
image: nginx
ports:
- "8080:80"
访问控制
配置访问策略
在 Zero Trust 中配置访问策略:
- 进入 "Access" → "Applications"
- 添加应用
- 配置访问策略:
- Email: 允许特定邮箱
- IP: 允许特定 IP
- Country: 允许特定国家
- Group: 允许特定用户组
启用访问控制
在隧道配置中启用 Access:
ingress:
- hostname: private.example.com
service: http://localhost:8080
originRequest:
access:
required: true
访问时需要通过身份验证。
多服务配置
tunnel: <tunnel-id>
credentials-file: /root/.cloudflared/<tunnel-id>.json
ingress:
# NAS 服务
- hostname: nas.example.com
service: http://192.168.1.100:5000
# Web 服务
- hostname: www.example.com
service: http://localhost:80
# API 服务
- hostname: api.example.com
service: http://localhost:3000
# SSH 服务
- hostname: ssh.example.com
service: ssh://localhost:22
# 默认返回 404
- service: http_status:404
负载均衡
ingress:
- hostname: app.example.com
service: http://localhost:8080
originRequest:
loadBalancer:
policy: round_robin
origins:
- url: http://localhost:8080
- url: http://localhost:8081
- url: http://localhost:8082
常见问题
隧道连接不上?
- 检查 cloudflared 是否正确安装
- 检查网络是否可以访问 Cloudflare
- 检查隧道 token 是否正确
服务无法访问?
- 检查本地服务是否正常运行
- 检查配置文件中的端口是否正确
- 检查 DNS 是否正确解析
如何查看日志?
# 查看服务日志
sudo journalctl -u cloudflared -f
# 或直接运行查看输出
cloudflared tunnel run my-tunnel
如何更新 cloudflared?
# Linux/macOS
cloudflared update
# Windows
winget upgrade cloudflare.cloudflared
安全建议
- 启用访问控制:不要让隧道完全公开,配置访问策略
- 使用 HTTPS:确保本地服务也使用 HTTPS
- 定期更新:保持 cloudflared 版本最新
- 监控日志:定期检查访问日志
参考链接
下一步
完成 Tunnel 学习后,接下来学习 DDoS 防护,了解如何保护网站免受攻击。