Cache Everything by Cloudflare

Cache Everything từ Cloudflare (Edge Cache HTML) là 1 hình thức cache đơn giản, siêu hiệu quả, giúp tăng tốc và giảm tải cực nhiều cho 1 trang WordPress thông thường

  • Layer 0 (L0): plugin caching thông thường, có thể ví dụ như FlyingPressWP-RocketSwift Performance ….trang web sẽ được chuyển về dạng tĩnh (html) lưu trên ổ cứng, L0 có thể kết hợp với L1 sử dụng Nginx để chạy trực tiếp (đây là cách trước đây mình hay dùng, tốc độ và chịu tải rất tốt, quản lý cũng rất đơn giản)
  • Layer 1 (L1): Web server (Apache/NGINX/OLS) thường các dịch vụ trả phí, giá đa phần trên $20/month mới có các gói này, có thể kể tới như LiteSpeed, NINGX SRCache, NGINX x-proxy, Runcloud, Kinsta, Shopify, Varnish … dạng này thường mình chẳng nhìn bao giờ vì mắc
  • Layer 2 (L2): CDN, có thể kể tới Amazon Cloudfront, Cloudflare (+APO), Fastly …. L2 có thể gọi là bố đời, mẹ thiên hạ về tốc độ và chịu tải, hình dung thì các trang sau khi cache về dạng tĩnh (html) sẽ được chuyển tới nhiều cụm CDN khác nhau, khách ở khu vực nào thì sẽ lấy dữ liệu từ cụm CDN đó, nên rất hiếm đụng chạm gì tới server gốc, lý thuyết L2 có thể cân cả thế giới theo đúng nghĩa đen

Ưu điểm mạnh nhất của Cache Everything của Cloudflare là nó cache ở tầng L2, hiệu quả hơn hẳn so với các hình thức cache qua PHP hay webserver như thông thường

Cấu hình trên Cloudflare

Hướng dẫn gốc của tác giả tại Github, bạn nào muốn hiểu rõ hơn thì có thể vào đó xem cụ thể

Truy cập vào Cache Rules

2023-06-24_10-06-08

Create new Cache Rule -> Edit Expression

2022-12-22-23-52-23

Chú ý thay thế haproxy.tech bằng domain của bạn ở các đoạn code bên dưới

Rule 1 ➜ Cache Bypass — WP Admin Paths, WooCommerce API, EDD API Endpoints

2023-06-24_9-26-08

  • Rule name (required): Cache Bypass — WP Admin Paths, WooCommerce API, EDD API Endpoints
  • Rule Expression: thêm dòng code bên dưới vào Use expression builder
(
  http.host eq "haproxy.tech" and
  (starts_with(http.request.uri.path, "/wp-admin") or starts_with(http.request.uri.path, "/wc-api/") or starts_with(http.request.uri.path, "/edd-api/"))
)
  • Cache status (required): Bypass cache

Xong xuôi thì ấn vào Deploy

Rule 2 ➜ Cache Bypass — XML, XSL & PHP Files

2023-06-24_9-28-32

  • Rule name (required): Cache Bypass — XML, XSL & PHP Files
  • Rule Expression: thêm dòng code bên dưới vào Use expression builder
(
  http.host eq "haproxy.tech" and
  (http.request.uri.path contains ".xsl" or http.request.uri.path contains ".xml" or http.request.uri.path contains ".php")
)
  • Cache status (required): Bypass cache

Xong xuôi thì ấn vào Deploy

Rule 3 ➜ Cache Bypass — Default Bypass Cookies

2023-06-24_9-29-50

  • Rule name (required): Cache Bypass — Default Bypass Cookies
  • Rule Expression: thêm dòng code bên dưới vào Use expression builder
(
  http.host eq "haproxy.tech" and
  (http.cookie contains "wordpress_logged_in_" or http.cookie contains "comment_" or http.cookie contains "woocommerce_" or http.cookie contains "wordpressuser_" or http.cookie contains "wordpresspass_" or http.cookie contains "wordpress_sec_" or http.cookie contains "yith_wcwl_products" or http.cookie contains "edd_items_in_cart" or http.cookie contains "it_exchange_session_" or http.cookie contains "comment_author" or http.cookie contains "dshack_level" or http.cookie contains "auth_" or http.cookie contains "noaffiliate_" or http.cookie contains "mp_session" or http.cookie contains "xf_" or http.cookie contains "mp_globalcart_") and
  not http.request.uri.path contains "."
)
  • Cache status (required): Bypass cache

Xong xuôi thì ấn vào Deploy

Rule 4 ➜ Cache Eligible Requests & Ignore Query Params from cacheKey

2023-06-24_9-32-35

  • Rule name (required): Cache Eligible Requests & Ignore Query Params from cacheKey
  • Rule Expression: thêm dòng code bên dưới vào Use expression builder
(http.host eq "haproxy.tech" and not starts_with(http.request.uri.path, "/wp-admin") and not starts_with(http.request.uri.path, "/wp-json/") and not starts_with(http.request.uri.path, "/wc-api/") and not starts_with(http.request.uri.path, "/edd-api/") and not http.request.uri.path contains ".xsl" and not http.request.uri.path contains ".xml" and not http.request.uri.path contains ".php" and not http.cookie contains "wordpress_logged_in_" and http.cookie ne "comment_" and not http.cookie contains "woocommerce_" and not http.cookie contains "wordpressuser_" and not http.cookie contains "wordpresspass_" and not http.cookie contains "wordpress_sec_" and not http.cookie contains "yith_wcwl_products" and not http.cookie contains "edd_items_in_cart" and not http.cookie contains "it_exchange_session_" and not http.cookie contains "comment_author" and not http.cookie contains "dshack_level" and not http.cookie contains "auth_" and not http.cookie contains "noaffiliate_" and not http.cookie contains "mp_session" and not http.cookie contains "xf_" and not http.cookie contains "mp_globalcart_")
  • Cache status (required): Eligible for cache

Cache key (optional):

  1. Bật Cache deception armor
  2. Bật Enable query string sort

Xong xuôi thì ấn vào Deploy

2023-06-24_9-39-03

Làm xong chúng ta sẽ có 4 Cache Rules như hình

Truy cập vào Page Rules

2023-06-24_10-07-41

Create Page Rule

2023-06-24_9-43-11

  • URL (required): điền vào https://haproxy.tech/*

Thay haproxy.tech bằng domain của bạn

Then the settings are:

  1. Pick a Setting (required): chọn Cache Level
    Select Cache Level (required): chọn Cache Everything
  2. Pick a Setting (required): chọn Edge Cache TTL
    Select Cache Level (required): chọn a month

Sau đó ấn vào Save and Deploy Page Rule

Cấu hình trên Wordpress

Download và cài đặt plugin Cloudflare

Điền email và Global API Key để login vào trang quản trị plugin Cloudflare

2023-06-24_9-51-51

Từ tab Settings đi tới tùy chọn Auto Purge Content On Update, bật ON

Các setting khác để mặc định

Kết bài 😀

Cấu hình Cache Everything Cloudflare về cơ bản là như thế

Lầu đầu làm có thể hơi dài dòng ở phần điều các rule cache, nhưng bạn chỉ phải làm duy nhất 1 lần, sau đó rất hiếm phải điều chỉnh thêm gì

Leave a Comment