このガイドに従って、API で Cloudflare Tunnel を設定します。
次の権限を持つ API トークンを作成 します。
| タイプ | 項目 | 権限 |
|---|---|---|
| Account | Cloudflare Tunnel | Edit |
| Zone | DNS | Edit |
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 の値をコピーします。トンネルの設定と実行に使います。
次の手順は、アプリケーションをインターネットに公開する か、プライベートネットワークを接続する かによって異なります。
トンネル経由でアプリケーションを公開する前に、次を完了してください。
アプリケーションをインターネットに公開するには、次の手順に従います。プライベートリソースを接続する場合は、ネットワークを接続する に進んでください。
-
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
Update Tunnel 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" } ] } }'イングレスルールの末尾には、キャッチオールルールが必要です。この例では、リクエストがそれより前のホスト名のいずれにも一致しない場合、
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 token エンドポイントでも取得できます。
-
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 のネットワークへの接続を 4 本持ちます。