מדריך קלאודפלייר בעברית

תיעוד 107

דוגמה ל-Terraform

הדוגמה הבאה מגדירה כלל Cache Response Rule יחיד לאזור באמצעות Terraform. הכלל מסיר את כותרות Set-Cookie ו-ETag מתגובות של קובצי JavaScript לפני האחסון במטמון.

#משאב Terraform cloudflare_ruleset

# Cache Response Rule to strip headers from JS responses
resource "cloudflare_ruleset" "cache_response_rules_example" {
  zone_id     = "<ZONE_ID>"
  name        = "Cache Response Rules"
  description = "Configure cache settings for origin responses"
  kind        = "zone"
  phase       = "http_response_cache_settings"

  rules {
    ref         = "strip_js_headers"
    description = "Strip caching headers from JS file responses"
    expression  = "http.request.uri.path.extension eq \"js\""
    action      = "set_cache_settings"
    action_parameters {
      strip_etags      = true
      strip_set_cookie = true
    }
  }

  rules {
    ref         = "tag_api_responses"
    description = "Tag API responses for targeted purging"
    expression  = "starts_with(http.request.uri.path, \"/api/\")"
    action      = "set_cache_tags"
    action_parameters {
      operation = "set"
      values    = ["api-response", "dynamic-content"]
    }
  }

  rules {
    ref         = "cache_control_200"
    description = "Set cache-control for successful responses"
    expression  = "http.response.code eq 200"
    action      = "set_cache_control"
    action_parameters {
      s_maxage {
        operation      = "set"
        value          = 86400
        cloudflare_only = true
      }
      must_revalidate {
        operation = "set"
      }
    }
  }
}

השתמשו בשדה ref כדי לקבל מזהי כללים יציבים בין עדכונים בעת שימוש ב-Terraform. הוספת שדה זה מונעת מ-Terraform ליצור מחדש את הכלל בעת שינויים. למידע נוסף, ראו פתרון בעיות בתיעוד של Terraform.

להנחיה נוספת על שימוש ב-Terraform עם Cloudflare, ראו Terraform.