有时候需要公网访问本地服务的场景,例如工作流调用测试。
本文记录创建cloudflared tunnel映射本地域名到公网。
比如我本地用的MAMP PRO,本地设置的域名是 apitest.com
临时隧道
1
| cloudflared tunnel --url http://127.0.0.1:80 --http-host-header apitest.com
|
控制台会输出临时域名,打开访问即可。
持久隧道(和临时隧道2选1)
这里 example.com 需替换为你在cloudflare自己的域名。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| cloudflared tunnel create apitest
创建配置文件 ~/.cloudflared/apitest.yml tunnel: 39856a68-71d3-4e54-b95a-8bed9cba6ce0 credentials-file: /Users/jun/.cloudflared/39856a68-71d3-4e54-b95a-8bed9cba6ce0.json
ingress: - hostname: apitest.example.com service: http://127.0.0.1:80 originRequest: httpHostHeader: apitest.com - service: http_status:404
配置DNS路由 cloudflared tunnel route dns apitest apitest.example.com
运行隧道 cloudflared tunnel --config ~/.cloudflared/apitest.yml run apitest
使用域名 apitest.example.com 访问。
|
常用命令
1 2 3 4 5
| 查看隧道列表 cloudflared tunnel list
删除隧道 cloudflared tunnel delete apitest
|