Tutorial Tutorial Application Services Application Services ~32 phút ~32 min Đồng bộ 2026-06-10 Synced 2026-06-10

5 – Add exceptions với Page Rules Add exceptions with Page Rules

Hướng dẫn chi tiết đồng bộ từ docs Cloudflare — mỗi section có backlink tới đúng vị trí trên trang gốc. Detailed guide synced from Cloudflare docs — each section links to the matching anchor on the official page.

← Danh mục ← Catalog

Giải thích nhanh Quick context

Thuộc nhóm Application Services — tập trung bảo vệ, tăng tốc và vận hành ứng dụng/web phía trước origin. Tutorial «5 – Add exceptions với Page Rules» giúp bạn làm quen luồng triển khai thật — phù hợp đọc trước khi mở tài liệu gốc tiếng Anh. Docs gốc chia khoảng 4 bước chính; bản tóm tắt dưới đây giúp bạn nắm khung trước khi làm theo từng lệnh.

Page Rules let you override zone settings for specific URL patterns. Redirects old URLs with a 301 permanent redirect.

Lưu ý trước khi làm Notes before you start

  • Đây là bản tóm tắt trên Orange Cloud Learning Hub — không thay thế tài liệu chính thức. This is a summary on Orange Cloud Learning Hub — it does not replace the official documentation.
  • Luôn mở liên kết «Tài liệu gốc» bên dưới khi cần lệnh CLI, snippet code và ảnh minh họa đầy đủ. Open the Official docs link below for CLI commands, code snippets, and full screenshots.
  • Kiểm tra token/API và state backend trước khi chạy trên môi trường production. Verify API tokens and the state backend before running against production.
  • Docs Cloudflare cập nhật thường xuyên — đối chiếu ngày «Rà soát lần cuối» trên trang gốc khi triển khai production. Cloudflare docs change frequently — verify the Last reviewed date on the official page before production use.

Tổng quan Overview

Phần «Tổng quan» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Overview" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗

In the Configure HTTPS settings tutorial, you configured zone settings that apply to all incoming requests for example.com. In this tutorial, you will add an exception to these settings using Page Rules.

Specifically, you will increase the security level for a URL known to be expensive to render and cannot be cached: https://www.example.com/expensive-db-call. Additionally, you will add a redirect from the previous URL used to host this page.

Terraform code snippets below refer to the v5 SDK only.

Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs)

1. Tạo Page Rules configuration 1. Create Page Rules configuration

Tạo new branch and append the configuration.

Read the "1. Create Page Rules configuration" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗

Create a new branch and append the configuration.

Terminal window

text
git checkout -b step5-pagerule

Page Rules let you override zone settings for specific URL patterns. Add two Page Rules to your main.tf:

text
# Increase security for expensive database operations

resource "cloudflare_page_rule" "expensive_endpoint_security" {

  zone_id  = var.zone_id

  target   = "${var.domain}/expensive-db-call"

  priority = 1


  actions = {

    security_level = "under_attack"

  }

}


# Redirect old URLs to new location

resource "cloudflare_page_rule" "legacy_redirect" {

  zone_id  = var.zone_id

  target   = "${var.domain}/old-location.php"

  priority = 2


  actions = {

    forwarding_url = {

      url         = "https://www.${var.domain}/expensive-db-call"

      status_code = 301

    }

  }

}

The first rule increases security to "Under Attack" mode for your database endpoint. The second rule redirects old URLs with a 301 permanent redirect.

2. Preview và apply changes: 2. Preview and apply the changes:

Phần «Preview và apply changes:» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "2. Preview and apply the changes:" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗

Terminal window

text
terraform plan

terraform apply

3. Xác minh changes: 3. Verify changes:

Phần «Xác minh changes:» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "3. Verify changes:" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗

Test the redirect functionality:

Terminal window

text
curl -I https://example.com/old-location.php

Expected output:

text
HTTP/1.1 301 Moved Permanently

Location: https://example.com/expensive-db-call

Test the increased security (Under Attack mode returns a challenge page):

Terminal window

text
curl -I https://example.com/expensive-db-call

Expected output:

text
HTTP/1.1 503 Service Temporarily Unavailable

The 503 response indicates the Under Attack mode is active, presenting visitors with a challenge page before allowing access to protect against DDoS attacks.

4. Commit và merge changes: 4. Commit and merge the changes:

Phần «Commit và merge changes:» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "4. Commit and merge the changes:" section below — open the official docs link for full screenshots and configuration tabs.

Mở section docs gốc ↗ Open source section ↗

Terminal window

text
git add main.tf

git commit -m "Step 5 - Add two Page Rules"

git push

The call works as expected. In the first case, the Cloudflare global network responds with a 301 redirecting the browser to the new location. In the second case, the Cloudflare global network initially responds with a 503, which is consistent with the Under Attack mode.

json
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"/directory/","name":"Directory"}},{"@type":"ListItem","position":2,"item":{"@id":"/terraform/","name":"Terraform"}},{"@type":"ListItem","position":3,"item":{"@id":"/terraform/tutorial/","name":"Tutorials"}},{"@type":"ListItem","position":4,"item":{"@id":"/terraform/tutorial/add-page-rules/","name":"5 – Add exceptions with Page Rules"}}]}

Xem bản đầy đủ trên developers.cloudflare.com (ảnh, tab cấu hình). View the full guide on developers.cloudflare.com (images, config tabs).

Tài liệu gốc ↗ Official docs ↗