Skip to content

Cloudflared本地管理使用手册

官方文档

常规使用

1、安装

安装说明

按照文档选择合适平台安装后登录:

sh
cloudflared tunnel login

登录成功后会在~/.cloudflared目录下创建一个cert.pem文件。

2、管理命令

sh
# 查看已创建的隧道列表
cloudflared tunnel list

# 删除无效隧道
cloudflared tunnel delete <TUNNEL_NAME>

# 本地创建隧道
cloudflared tunnel create <TUNNEL_NAME>

3、DNS

sh
cloudflared tunnel route dns <TUNNEL_NAME> domain1.example.com

更多ip相关的查询Start routing traffic

配置文件config.yml写法:

yml
tunnel: xxxxx-xxx-xxx-xxx-xxxxx  # 隧道ID
credentials-file: /path/to/<隧道ID>.json  # 凭证文件路径
ingress:
  # 域名1指向本地Web服务
  - hostname: domain1.example.com
    service: http://localhost:8080

  # 域名2指向另一个端口服务
  - hostname: domain2.example.com
    service: http://localhost:3000

  # 子域名指向TCP服务(如SSH)
  - hostname: sub.domain3.example.com
    service: tcp://localhost:22

  # 兜底规则(必须保留)
  - service: http_status:404

更多配置文件细节查看文档Configuration file

4、启动隧道

sh
# 基于config.yml
cloudflared tunnel --config ~/.cloudflared/config.yml run <TUNNEL_NAME>

5、测试连通性

sh
# 测试域名是否命中规则
cloudflared tunnel ingress rule https://domain1.example.com

macOS上daemon方式

1、用户模式运行(推荐)

sh
# 启动并设为Login自启
launchctl load ~/Library/LaunchAgents/com.cloudflare.cloudflared.plist

# 停止服务
launchctl unload ~/Library/LaunchAgents/com.cloudflare.cloudflared.plist

# 查看服务是否在运行
launchctl list | grep cloudflared

2、macOS上的daemon运行方式:

sh
# 启动并设为开机自启
sudo launchctl load /Library/LaunchDaemons/com.cloudflare.cloudflared.plist

# 停止服务
sudo launchctl unload /Library/LaunchDaemons/com.cloudflare.cloudflared.plist

# 查看服务是否在运行
sudo launchctl list | grep cloudflared

root方式需要把config.yml拷贝到/etc/cloudflared目录。

日志查看

sh
log show --predicate 'process == "cloudflared"' --last 5m