コンテンツにスキップ

変換ルール

このページでは、Terraformを使用してゾーン内で変換ルールを作成する例を提供します。例は以下のシナリオをカバーしています:

変換ルールに関する詳細は、変換ルールを参照してください。

始める前に

必要なアカウントまたはゾーンIDを取得する

The Terraform configurations provided in this page need the zone ID (or account ID) of the zone/account where you will deploy rulesets.

  • To retrieve the list of accounts you have access to, including their IDs, use the List accounts operation.
  • To retrieve the list of zones you have access to, including their IDs, use the List zones operation.

既存のルールセットをインポートまたは削除する

Terraform assumes that it has complete control over account and zone rulesets. If you already have rulesets configured in your account or zone, do one of the following:

  • Import existing rulesets to Terraform using the cf-terraforming tool. Recent versions of the tool can generate resource definitions for existing rulesets and import their configuration to Terraform state.
  • Start from scratch by deleting existing rulesets (account and zone rulesets with "kind": "root" and "kind": "zone", respectively) and then defining your rulesets configuration in Terraform.

リライトURLルールの作成

以下の例は、example.com/old-folderへのリクエストをexample.com/new-folderにリライトするリライトURLルールを作成します:

resource "cloudflare_ruleset" "transform_url_rewrite" {
zone_id = "<ZONE_ID>"
name = "静的URLリライトを行う変換ルール"
description = ""
kind = "zone"
phase = "http_request_transform"
rules {
action = "rewrite"
action_parameters {
uri {
path {
value = "/new-folder"
}
}
}
expression = "(http.host eq \"example.com\" and http.request.uri.path eq \"/old-folder\")"
description = "例のリライトURLルール"
enabled = true
}
}

To create another リライトURLルール, add a new rules object to the same cloudflare_ruleset resource.


URLのリライトに関する詳細は、リライトURLルールを参照してください。

HTTPリクエストヘッダー修正ルールの作成

以下の構成例は、HTTPリクエストヘッダーに対して以下の調整を行います:

  • 静的値を持つmy-header-1ヘッダーをリクエストに追加します。
  • 式によって定義された動的値を持つmy-header-2ヘッダーをリクエストに追加します。
  • 既存のexisting-headerヘッダーをリクエストから削除します(存在する場合)。
resource "cloudflare_ruleset" "transform_modify_request_headers" {
zone_id = "<ZONE_ID>"
name = "HTTPリクエストヘッダー修正を行う変換ルール"
description = ""
kind = "zone"
phase = "http_request_late_transform"
rules {
action = "rewrite"
action_parameters {
headers {
name = "my-header-1"
operation = "set"
value = "固定値"
}
headers {
name = "my-header-2"
operation = "set"
expression = "cf.zone.name"
}
headers {
name = "existing-header"
operation = "remove"
}
}
expression = "true"
description = "例のHTTPリクエストヘッダー修正ルール"
enabled = true
}
}

To create another リクエストヘッダー修正ルール, add a new rules object to the same cloudflare_ruleset resource.


リクエストヘッダーの修正に関する詳細は、HTTPリクエストヘッダー修正ルールを参照してください。

HTTPレスポンスヘッダー修正ルールの作成

以下の構成例は、HTTPレスポンスヘッダーに対して以下の調整を行います:

  • 静的値を持つmy-header-1ヘッダーをレスポンスに追加します。
  • 式によって定義された動的値を持つmy-header-2ヘッダーをレスポンスに追加します。
  • 既存のexisting-headerヘッダーをレスポンスから削除します(存在する場合)。
resource "cloudflare_ruleset" "transform_modify_response_headers" {
zone_id = "<ZONE_ID>"
name = "HTTPレスポンスヘッダー修正を行う変換ルール"
description = ""
kind = "zone"
phase = "http_response_headers_transform"
rules {
action = "rewrite"
action_parameters {
headers {
name = "my-header-1"
operation = "set"
value = "固定値"
}
headers {
name = "my-header-2"
operation = "set"
expression = "cf.zone.name"
}
headers {
name = "existing-header"
operation = "remove"
}
}
expression = "true"
description = "例のHTTPレスポンスヘッダー修正ルール"
enabled = true
}
}

To create another レスポンスヘッダー修正ルール, add a new rules object to the same cloudflare_ruleset resource.


レスポンスヘッダーの修正に関する詳細は、HTTPレスポンスヘッダー修正ルールを参照してください。