一部のページは無料アクセスにし、ほかのページだけ課金したい場合があります。
- ホームページ、カテゴリページ、ナビゲーション は無料にして、クローラーが有料コンテンツを見つけやすくします。
- ログイン、検索、API エンドポイント など、課金対象のコンテンツがない機能ページは除外します。
- まずはサイトの 一部だけ で Pay Per Crawl を始め、あとから範囲を広げます。
- プロモーションやアーカイブ は無料にし、プレミアム記事には課金します。
はじめに、Configuration Rules で、特定の URI パターンを課金対象から外します。
-
Cloudflare ダッシュボードで Rules > Overview を開きます。
Overview を開く ↗ -
Create rule > Configuration Rule を選択します。
-
When incoming requests match: URI パターンを設定します。
- Field:
URI Full - Operator:
wildcard - Value:
https://*example.com/public/*
- Field:
-
Disable Pay Per Crawl > Add を選択します。
-
Deploy を選択します。
パターンの例:
- ホームページを無料にする:
URI Fullがhttps://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: リクエストが支払い対象でないとき(ボット以外など)。
オリジンはそのままにしたい場合は、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;
}
};