Skip to content

非公式 / UnofficialCloudflare, Inc. 非提携。アカウント情報は入力しないでください。Not affiliated with Cloudflare, Inc. Do not enter Cloudflare credentials here.Official docsこのサイトについてプライバシー

HTTP Host ヘッダーと DNS レコードを変更する

Origin Rule を作成し、HTTP Host ヘッダーと DNS レコードを変更します。

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

次の Origin Rule は、URI パスが /hr-app/ で始まるすべてのリクエストに対し、HTTP Host ヘッダーを hr-server.example.com に上書きします。DNS レコードも同じホスト名に上書きします。

Host ヘッダーの上書きはヘッダー値だけを更新します。受信リクエストの振り直しは DNS レコードの上書きが担当します。これらの上書きの詳細は Origin Rules の設定 を参照してください。

Expression Builder を使う場合の式:

Field Operator Value
URI Path starts with /hr-app/

Expression Editor を使う場合の式:

(starts_with(http.request.uri.path, "/hr-app/"))

Host Header > Rewrite to の値:

hr-server.example.com

DNS Record > Override to の値:

hr-server.example.com

次の例では、ゾーンルールセットを更新する 操作を使い、既存のフェーズルールセット($RULESET_ID)のルールをオリジンルール 1 件に設定します。このルールは、受信リクエストの Host ヘッダーと解決先の DNS レコードを上書きします。レスポンスには、更新したルールセットの完全な定義が含まれます。

Required API token permissions

At least one of the following token permissions is required:
  • Response Compression Write
  • Config Settings Write
  • Dynamic URL Redirects Write
  • Cache Settings Write
  • Custom Errors Write
  • Origin Write
  • Managed headers Write
  • Zone Transform Rules Write
  • Mass URL Redirects Write
  • Magic Firewall Write
  • L4 DDoS Managed Ruleset Write
  • HTTP DDoS Managed Ruleset Write
  • Sanitize Write
  • Transform Rules Write
  • Select Configuration Write
  • Bot Management Write
  • Zone WAF Write
  • Account WAF Write
  • Account Rulesets Write
  • Logs Write
  • Logs Write
Update a zone rulesetbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/rulesets/$RULESET_ID" \
	--request PUT \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"rules": [
				{
						"ref": "hr_app_overrides",
						"expression": "starts_with(http.request.uri.path, \"/hr-app/\")",
						"description": "Origin rule for the company HR application",
						"action": "route",
						"action_parameters": {
								"host_header": "hr-server.example.com",
								"origin": {
										"host": "hr-server.example.com"
								}
						}
				}
		]
	}'
{
	"result": {
		"id": "<RULESET_ID>",
		"name": "Origin Rules ruleset",
		"description": "Zone-level ruleset that will execute origin rules.",
		"kind": "zone",
		"version": "2",
		"rules": [
			{
				"ref": "hr_app_overrides",
				"id": "<RULE_ID>",
				"version": "1",
				"action": "route",
				"action_parameters": {
					"host_header": "hr-server.example.com",
					"origin": {
						"host": "hr-server.example.com"
					}
				},
				"expression": "starts_with(http.request.uri.path, \"/hr-app/\")",
				"description": "Origin rule for the company HR application",
				"last_updated": "2022-06-03T14:42:04.219025Z",
				"ref": "<RULE_REF>"
			}
		],
		"last_updated": "2022-06-03T14:42:04.219025Z",
		"phase": "http_request_origin"
	},
	"success": true,
	"errors": [],
	"messages": []
}

Terraform を使う場合、更新をまたいでルール ID を安定させるには ref フィールドを使います。このフィールドを追加すると、変更時に Terraform がルールを再作成するのを防げます。詳細は Terraform ドキュメントの トラブルシューティング を参照してください。

役に立ちましたか?