cloudflare_page_rule

Provides a Cloudflare page rule resource.

Example Usage

# Add a page rule to the domain
resource "cloudflare_page_rule" "foobar" {
  zone_id = var.cloudflare_zone_id
  target = "sub.${var.cloudflare_zone}/page"
  priority = 1

  actions {
    ssl = "flexible"
    email_obfuscation = "on"
    minify {
      html = "off"
      css  = "on"
      js   = "on"
    }
  }
}

Argument Reference

The following arguments are supported:

Action blocks support the following:

Forwarding URL actions support the following:

Minify actions support the following:

Cache Key Fields

A Cache Key is an identifier that Cloudflare uses for a file in a cache. The Cache Key Template defines this identifier for a given HTTP request.

For detailed description of use cases and semantics for the particular setting please refer to Cloudflare Support article.

Example:

# Cache JavaScript files:
# - ignore CORS Origin header (one copy regardless of requesting Host)
# - ignore API key query string
# - include browser language preference (e.g. string translations)
resource "cloudflare_page_rule" "foobar" {
  zone_id = var.cloudflare_zone_id
  target = "embed.${var.cloudflare_zone}/*.js"
  priority = 1

  actions {
    cache_key_fields {
      header {
        exclude = ["origin"]
      }
      query_string {
        exclude = ["api_token"]
      }
      user {
        lang = true
      }
      cookie {}
      host {}
    }
  }
}

Example:

# Unrealistic example with all features used
resource "cloudflare_page_rule" "foobar" {
  zone_id = var.cloudflare_zone_id
  target = "${var.cloudflare_zone}/app/*"
  priority = 1

  actions {
    cache_key_fields {
      cookie {
        check_presence = ["wordpress_test_cookie"]
      }
      header {
        check_presence = ["header_present"]
        exclude = ["origin"]
        include = ["api-key", "dnt"]
      }
      host {
        resolved = true
      }
      query_string {
        ignore = true
      }
      user {
        device_type = false
        geo = true
        lang = true
      }
    }
  }
}

Cache TTL by status

Set cache TTL based on the response status from the origin web server. Cache TTL (time-to-live) refers to the duration a resource lives in the Cloudflare network before it is marked as stale or discarded from cache. Status codes are returned by a resource’s origin.

For detailed description please refer to Cloudflare Support article.

Example:

resource "cloudflare_page_rule" "test" {
    zone_id = var.cloudflare_zone_id
    target = "${var.cloudflare_zone}/app/*"
    priority = 1

    actions {
        cache_ttl_by_status {
            codes = "200-299"
            ttl = 300
        }
        cache_ttl_by_status {
            codes = "300-399"
            ttl = 60
        }
        cache_ttl_by_status {
            codes = "400-403"
            ttl = -1
        }
        cache_ttl_by_status {
            codes = "404"
            ttl = 30
        }
        cache_ttl_by_status {
            codes = "405-499"
            ttl = -1
        }
        cache_ttl_by_status {
            codes = "500-599"
            ttl = 0
        }
    }
}

Attributes Reference

The following attributes are exported:

Import

Page rules can be imported using a composite ID formed of zone ID and page rule ID, e.g.

$ terraform import cloudflare_page_rule.default d41d8cd98f00b204e9800998ecf8427e/ch8374ftwdghsif43