Linux 系统
#Debian 和 Ubuntu APT
cloudflared使用 apt 包管理器在兼容的计算机上安装。
- 添加 Cloudflare 的包签名密钥:
sudo mkdir -p --mode=0755 /usr/share/keyrings
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
- 将 Cloudflare 的 apt 存储库添加到您的 apt 存储库:
echo "deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/cloudflared.list
- 更新存储库并安装 cloudflared:
sudo apt-get update && sudo apt-get install cloudflared
接下来登陆认证
cloudflared tunnel login
运行此命令将:
创建隧道名称
cloudflared tunnel create <NAME>
运行此命令将:
从命令的输出中,记下隧道的 UUID 和隧道凭证文件的路径。
创建配置文件
- 在您的目录中,使用任何文本编辑器
.cloudflared创建一个config.yml文件。该文件将配置隧道以将流量从给定来源路由到您选择的主机名。 - 将以下字段添加到文件中:
如果您要连接应用程序:
url: http://localhost:8000
tunnel: <Tunnel-UUID>
credentials-file: /root/.cloudflared/<Tunnel-UUID>.json
如果您连接的是专用网络:
tunnel: <Tunnel-UUID>
credentials-file: /root/.cloudflared/<Tunnel-UUID>.json
warp-routing:
enabled: true
开始路由流量
- 现在分配一条CNAME记录,将流量指向您的隧道子域:
- 如果您要连接应用程序,请将服务路由到公共主机名:
cloudflared tunnel route dns <UUID or NAME> <hostname>
- 如果您要连接专用网络,请通过隧道路由 IP 地址或 CIDR:
cloudflared tunnel route ip add <IP/CIDR> <UUID or NAME>
- 确认路由已成功建立:
cloudflared tunnel route ip show
运行隧道
运行隧道以代理从隧道到源本地运行的任意数量的服务的传入流量。
cloudflared tunnel run <UUID or NAME>
如果您的配置文件具有自定义名称或不在.cloudflared目录中,请添加--config标志并指定路径。
cloudflared tunnel --config /path/your-config-file.yml run <UUID or NAME>
Cloudflare Tunnel 可以将自身安装为 Linux 和 Windows 上的系统服务,以及 macOS 上的启动代理。有关详细信息,请参阅作为服务运行。
详细信息可参考官网文档
配置文件:
tunnel: 隧道ID
credentials-file: /etc/cloudflared/credentials.json
originCert: /etc/cloudflared/cert.pem # 确保源证书路径正确
ingress:
- hostname: 域名 # 替换为您的域名
service: http://8.8.8.8:8080 # 替换为您的本地服务地址
originRequest:
http2: true # 启用 HTTP/2
- service: http_status:404 # 默认返回 404 错误
cloudflare更新
如果通过 apt 安装:
- 更新包:
cloudflaredsudo apt-get update && sudo apt-get upgrade cloudflared
- 重新启动服务:
sudo systemctl restart cloudflared.service