Skip to content

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

詳細設定

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

URI パターンで Pay Per Crawl を無効にする

一部のページは無料アクセスにし、ほかのページだけ課金したい場合があります。

  • ホームページ、カテゴリページ、ナビゲーション は無料にして、クローラーが有料コンテンツを見つけやすくします。
  • ログイン、検索、API エンドポイント など、課金対象のコンテンツがない機能ページは除外します。
  • まずはサイトの 一部だけ で Pay Per Crawl を始め、あとから範囲を広げます。
  • プロモーションやアーカイブ は無料にし、プレミアム記事には課金します。

はじめに、Configuration Rules で、特定の URI パターンを課金対象から外します。

  1. Cloudflare ダッシュボードで Rules > Overview を開きます。

    Overview を開く ↗
  2. Create rule > Configuration Rule を選択します。

  3. When incoming requests match: URI パターンを設定します。

    • Field: URI Full
    • Operator: wildcard
    • Value: https://*example.com/public/*
  4. Disable Pay Per Crawl > Add を選択します。

  5. Deploy を選択します。

パターンの例:

  • ホームページを無料にする: URI Fullhttps://example.com/ と一致
  • ディレクトリを無料にする: URI Full の wildcard が https://*example.com/public/*

動的料金

Pay Per Crawl 設定で指定した料金は、デフォルトではゾーン全体に適用されます。差別化した料金ポリシーにするには Enable dynamic pricing を選び、オリジンの HTTP レスポンスに crawler-price ヘッダーを含めます。例:

crawler-price: USD 3.14

レスポンスに crawler-price ヘッダーがある場合、ゾーンの Pay Per Crawl 設定のデフォルト料金ではなく、そのヘッダーの料金が使われます。

動的料金用のリクエストヘッダー

Pay Per Crawl は、すべてのオリジンリクエストに cf-pay-per-crawl ヘッダーを付けます。このヘッダーは適用中の料金モードを示します。オリジンはこれを見て、レスポンスに crawler-price ヘッダーを付けるかどうかを判断できます。

cf-pay-per-crawl: protocol=cloudflare, pricing=in-band

現時点で protocol インジケーターの値は cloudflare のみです。pricing インジケーターの値は次のいずれかです。

  • zone-default: ゾーンで in-band 料金が有効になっていないとき。
  • in-band: ゾーンで動的料金が有効なとき。
  • bypass: リクエストが支払い対象でないとき(ボット以外など)。

動的料金に Workers を使う

オリジンはそのままにしたい場合は、Worker でレスポンスに crawler-price ヘッダーを付けられます。Worker では、受信リクエストのプロパティ(Cloudflare グローバルネットワークが付けた情報を含む)やコンテンツ自体にもとづいて料金を選べます。

次の Worker スクリプトは、リクエストされた URL パスにもとづいて料金を選ぶ簡単なポリシーの例です。Cloudflare Cache もそのまま使えます。

function getContentPriceUSD(request, response) {
	const requestPath = new URL(request.url).pathname;

	if (requestPath.startsWith("/premium-content/")) {
		return 3.14;
	}

	if (requestPath.startsWith("/free-content/")) {
		return 0.0;
	}

	return null; // Use the default price set in the zone configuration.
}

export default {
	async fetch(request, env, ctx) {
		// Obtain the response first (and allow it to be cached if possible).
		let response = await fetch(request, { cf: { cacheEverything: true } });

		// Indicates the pricing mode in effect ("bypass", "zone-default", "in-band").
		const cfPayPerCrawl = request.headers.get("CF-Pay-Per-Crawl") || "";

		// If in-band pricing is enabled, use the request/response to select a price.
		if (cfPayPerCrawl.match(/\bpricing=in-band\b/)) {
			const contentPrice = getContentPriceUSD(request, response);

			if (contentPrice !== null) {
				// Make the response mutable, to allow setting the price header.
				response = new Response(response.body, response);
				response.headers.set("Crawler-Price", `USD ${contentPrice.toFixed(2)}`);
			}
		}
		return response;
	}
};

関連リソース

役に立ちましたか?