Skip to content

非公式本サイトは非公式の日本語ドキュメントであり、Cloudflare 公式サイトではありません。最新情報はdevelopers.cloudflare.comをご確認ください。

Workers を Cloudflare Mesh に接続する

最終更新 Markdown で表示Agent セットアップ

この例では、VPC Network バインディングと Cloudflare Mesh(旧 WARP Connector)を使い、アカウント内の任意のプライベートサービスへ Worker から接続します。個別ホストの事前登録や Cloudflare Tunnel UUID の指定は不要です。

network_id: "cf1:network"Cloudflare Mesh にバインドすると、Worker は任意の Mesh ノード、クライアントデバイス、Cloudflare Tunnel または Cloudflare Mesh 経由で広報されたサブネットまたはホスト名ルート、あるいは Cloudflare WAN オンランプ(GRE、IPsec、CNI)経由で接続された宛先に到達できます。

前提条件

  • プライベートネットワークに接続した Cloudflare Mesh ノード
  • Mesh ノード背後で動くプライベートサービス(内部 API、データベース、Web アプリケーションなど)

1. Worker を設定する

Wrangler 設定で network_id: "cf1:network" を使い、Worker を Cloudflare Mesh にバインドします。

{
	"$schema": "./node_modules/wrangler/config-schema.json",
	"name": "mesh-gateway",
	"main": "src/index.js",
	// Set this to today's date
	"compatibility_date": "2026-09-20",
	"vpc_networks": [
		{
			"binding": "MESH",
			"network_id": "cf1:network",
			"remote": true
		}
	]
}
"$schema" = "./node_modules/wrangler/config-schema.json"
name = "mesh-gateway"
main = "src/index.js"
# Set this to today's date
compatibility_date = "2026-09-20"

[[vpc_networks]]
binding = "MESH"
network_id = "cf1:network"
remote = true

この 1 つのバインディングで、Worker はアカウント内のすべての Cloudflare Tunnel、Mesh ノード、Cloudflare WAN オンランプ上の任意のサービスに到達できます。

2. Worker を実装する

VPC Network バインディングを使い、プライベート IP アドレスでサービスにアクセスします。Cloudflare Mesh は現在、IP ベースのルーティングのみに対応しています。

index.jsjs
// You can target a Mesh node directly by its Mesh IP or any private IP
// on a subnet route behind the node
const SERVICE_IP = "10.0.1.50";
const SERVICE_PORT = 8080;

export default {
	async fetch(request, env, ctx) {
		try {
			const response = await env.MESH.fetch(
				`http://${SERVICE_IP}:${SERVICE_PORT}/api/data`,
			);
			return response;
		} catch (error) {
			// fetch() throws if the VPC Network cannot connect to the target
			return new Response("Service unavailable", { status: 503 });
		}
	},
};

VPC Services とは異なり、fetch() に渡す URL または connect() に渡すアドレスが実際の宛先を決めます。サービスごとに別のバインディングを作らずに、Mesh ネットワーク経由で到達できる任意の IP とポートにアクセスできます。

TCP 接続

同じバインディングの connect() で、非 HTTP サービスへの生の TCP ソケットも開けます。

index.jsjs
// You can target a Mesh node directly by its Mesh IP or any private IP
// on a subnet route behind the node
const REDIS_IP = "10.0.1.50";
const REDIS_PORT = 6379;

export default {
	async fetch(request, env, ctx) {
		try {
			const socket = await env.MESH.connect(`${REDIS_IP}:${REDIS_PORT}`);

			const writer = socket.writable.getWriter();
			await writer.write(new TextEncoder().encode("PING\r\n"));
			await writer.close();

			return new Response(socket.readable);
		} catch (error) {
			// connect() throws if the VPC Network cannot connect to the target
			return new Response("Service unavailable", { status: 503 });
		}
	},
};

3. デプロイしてテストする

Worker をデプロイし、プライベートサービスへ到達できることを確認します。

npx wrangler deploy
# Test accessing the internal user API
curl https://mesh-gateway.workers.dev/api/users

# Test accessing metrics by private IP
curl https://mesh-gateway.workers.dev/api/metrics

次のステップ

役に立ちましたか?