按照此指南使用 API 设置 Cloudflare Tunnel。
创建 API 令牌并授予以下权限:
| 类型 | 项目 | 权限 |
|---|---|---|
| 账户 | Cloudflare Tunnel | 编辑 |
| 区域 | DNS | 编辑 |
向 Cloudflare Tunnel 端点发送 POST 请求:
Required API token permissions
At least one of the following token permissions is required:Cloudflare One Connectors WriteCloudflare One Connector: cloudflared WriteCloudflare Tunnel Write
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/cfd_tunnel" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "api-tunnel",
"config_src": "cloudflare"
}'{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "c1744f8b-faa1-48a4-9e5c-02ac921467fa",
"account_tag": "699d98642c564d2e855e9661899b7252",
"created_at": "2025-02-18T22:41:43.534395Z",
"deleted_at": null,
"name": "example-tunnel",
"connections": [],
"conns_active_at": null,
"conns_inactive_at": "2025-02-18T22:41:43.534395Z",
"tun_type": "cfd_tunnel",
"metadata": {},
"status": "inactive",
"remote_config": true,
"credentials_file": {
"AccountTag": "699d98642c564d2e855e9661899b7252",
"TunnelID": "c1744f8b-faa1-48a4-9e5c-02ac921467fa",
"TunnelName": "api-tunnel",
"TunnelSecret": "bTSquyUGwLQjYJn8cI8S1h6M6wUc2ajIeT7JotlxI7TqNqdKFhuQwX3O8irSnb=="
},
"token": "eyJhIjoiNWFiNGU5Z..."
}
}复制输出中显示的 id 和 token 值。您将需要 these 选项配置并运行隧道。
接下来的步骤取决于您是要向互联网发布应用程序还是连接网络。
在通过隧道发布应用程序之前,您必须:
按照以下步骤向互联网发布应用程序。如果您要连接私有资源,请跳至连接网络部分。
-
发送
PUT请求以将您的本地服务 URL 路由到公共主机名。例如:
At least one of the following token permissions is required:Required API token permissions
Cloudflare One Connectors WriteCloudflare One Connector: cloudflared WriteCloudflare Tunnel Write
Put configurationbash curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/cfd_tunnel/$TUNNEL_ID/configurations" \ --request PUT \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --json '{ "config": { "ingress": [ { "hostname": "app.example.com", "service": "http://localhost:8001", "originRequest": {} }, { "service": "http_status:404" } ] } }'您的 Ingress 规则必须在末尾包含一个通配规则。在此示例中,当请求与之前的所有主机名都不匹配时,
cloudflared将响应 404 状态码。 -
为您的应用程序创建 DNS 记录:
At least one of the following token permissions is required:Required API token permissions
DNS Write
Create DNS Recordbash curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records" \ --request POST \ --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ --json '{ "type": "CNAME", "proxied": true, "name": "app.example.com", "content": "c1744f8b-faa1-48a4-9e5c-02ac921467fa.cfargotunnel.com" }'此 DNS 记录允许 Cloudflare 将
app.example.com流量代理到您的 Cloudflare Tunnel(<tunnel-id>.cfargotunnel.com)。
一旦您运行隧道,此应用程序将在互联网上公开可用。要允许或阻止特定用户,请创建 Access 应用程序。
要通过隧道连接私有网络,请添加隧道路由:
Required API token permissions
At least one of the following token permissions is required:Cloudflare One Networks WriteCloudflare Tunnel Write
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/teamnet/routes" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"network": "172.16.0.0/16",
"tunnel_id": "c1744f8b-faa1-48a4-9e5c-02ac921467fa",
"comment": "Example private network route"
}'cloudflared 现在可以将流量路由到这些目标 IP。若要配置 Zero Trust 策略并以用户身份进行连接,请参阅连接私有网络。
在您的服务器上安装 cloudflared,并使用在 2. 创建隧道中获取的 token 值运行该隧道。您也可以使用 Cloudflare Tunnel 令牌端点来获取隧道令牌。
-
下载并安装 ↗
cloudflared。 -
运行以下命令:
sudo cloudflared service install <TUNNEL_TOKEN>
-
下载并安装
cloudflared。 -
以管理员身份打开命令提示符。
-
运行以下命令:
cloudflared.exe service install <TUNNEL_TOKEN>
-
下载并安装
cloudflared。 -
打开终端窗口并运行以下命令:
sudo cloudflared service install <TUNNEL_TOKEN>
-
打开终端窗口。
-
运行以下命令:
docker run cloudflare/cloudflared:latest tunnel --no-autoupdate run --token <TUNNEL_TOKEN>
若要检查隧道是否正在提供流量服务:
Required API token permissions
At least one of the following token permissions is required:Cloudflare One Connectors WriteCloudflare One Connectors ReadCloudflare One Connector: cloudflared WriteCloudflare One Connector: cloudflared ReadCloudflare Tunnel WriteCloudflare Tunnel Read
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/cfd_tunnel/c1744f8b-faa1-48a4-9e5c-02ac921467fa" \
--request GET \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN"{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "c1744f8b-faa1-48a4-9e5c-02ac921467fa",
"account_tag": "699d98642c564d2e855e9661899b7252",
"created_at": "2025-02-18T22:41:43.534395Z",
"deleted_at": null,
"name": "example-tunnel",
"connections": [
{
"colo_name": "bos01",
"uuid": "2xz99mfm-a59e-4924-gyh9-z9vafaw6k0i2",
"id": "2xz99mfm-a59e-4924-gyh9-z9vafaw6k0i2",
"is_pending_reconnect": false,
"origin_ip": "10.1.0.137",
"opened_at": "2025-02-19T19:11:12.101642Z",
"client_id": "4xh4eb3f-cz0j-2aso-hu6i-36207018771a",
"client_version": "2025.2.0"
},
{
"colo_name": "phl01",
"uuid": "axe2socu-2fb5-3akx-b860-898zyes3cs9q",
"id": "axe2socu-2fb5-3akx-b860-898zyes3cs9q",
"is_pending_reconnect": false,
"origin_ip": "10.1.0.137",
"opened_at": "2025-02-19T19:11:12.006297Z",
"client_id": "4xh4eb3f-cz0j-2aso-hu6i-36207018771a",
"client_version": "2025.2.0"
},
{
"colo_name": "phl01",
"uuid": "9b5y0wm9-ca7f-ibq6-8ff4-sm53xekfyym1",
"id": "9b5y0wm9-ca7f-ibq6-8ff4-sm53xekfyym1",
"is_pending_reconnect": false,
"origin_ip": "10.1.0.137",
"opened_at": "2025-02-19T19:11:12.004721Z",
"client_id": "4xh4eb3f-cz0j-2aso-hu6i-36207018771a",
"client_version": "2025.2.0"
},
{
"colo_name": "bos01",
"uuid": "g6cdeiz1-80f5-3akx-b18b-3y0ggktoxwkd",
"id": "g6cdeiz1-80f5-3akx-b18b-3y0ggktoxwkd",
"is_pending_reconnect": false,
"origin_ip": "10.1.0.137",
"opened_at": "2025-02-19T19:11:12.110765Z",
"client_id": "4xh4eb3f-cz0j-2aso-hu6i-36207018771a",
"client_version": "2025.2.0"
}
],
"conns_active_at": "2025-02-19T19:11:12.004721Z",
"conns_inactive_at": null,
"tun_type": "cfd_tunnel",
"metadata": {},
"status": "healthy",
"remote_config": true
}
}正常的隧道将与 Cloudflare 的网络建立四个连接。