没有不值得去解决的问题,也没有不值得去学习的技术!

Recommended WordPress and Cloudflare Configuration With Practical Cache Rules Optimization

图12:评论实时生效

作者:

在

🧭 1. Background: Why a Second Optimization Article is Needed

In the previous article, Cloudflare SSL/TLS and Cache Rules Configuration in Practice, I completed the basic rule setup, including:

✔ 5 Core Rules Completed:

  • Bypass cache for WordPress admin dashboard
  • Bypass cache for WordPress login page
  • Bypass cache for WordPress REST API
  • Long-term caching for static assets (CSS / JS)
  • Static asset cache optimization

🚀 New Goals in This Article:

Building on the existing foundation, I am adding one key rule:

🧠 HTML Page Caching (SEO-Safe Optimization)


🧩 2. Overall Architecture Upgrade Overview

Plaintext
WordPress (Content Origin)
        ↓
Cloudflare Plugin (Auto Purge)
        ↓
Cache Rules (Fine-grained Control Layer)
        ↓
Cloudflare Edge Cache (HIT/MISS)

⚙️ 3. Cloudflare Recommended Configuration (Apply Recommended)


📌 Figure 1: WordPress Cloudflare Plugin Settings Entry

Figure 1: WordPress Cloudflare plugin settings entry

📌 Figure 2: Clicking Apply Recommended Cloudflare Settings for WordPress

Figure 2: Clicking Apply Recommended Cloudflare Settings for WordPress

🧠 Key Notes (Important Correction)

This action:

❗ Does not enable all available settings
❗ Does not override Cache Rules


📌 Figure 3: Prompt indicating “All available settings are enabled”

Figure 3: Prompt indicating "All available settings are enabled"

🧠 Key Conclusions:

  • Recommended configuration ≠ enabling everything; I manually enabled all settings in the Cloudflare dashboard myself
  • Recommended configuration = Zone Settings API write

🚨 4. Cloudflare Plugin Exception (Key Pitfall)


📌 Figure 4: WordPress Cloudflare Settings Page Loading Exception (Forbidden)

Figure 4: WordPress Cloudflare settings page loading exception (Forbidden)

❌ Symptom:

  • Page stuck on Loading
  • Console error:
Plaintext
Forbiddenr.body.result is null

🧠 Analysis Process:

Via API call:

Plaintext
/wp-admin/admin-ajax.php?action=cloudflare_proxy

Returns:

JSON
{
  "result": null,
  "success": false,
  "message": "Forbidden"}
}

🔍 Root Cause Analysis:

Missing Cloudflare API permissions


✔ Resolution Process:

Gradually added permissions:

  • Zone Read
  • Zone Settings Read/Write
  • Cache Purge
  • DNS Read (auxiliary API)

📌 Figure 5: Plugin returns to normal after permission fix, final permission configuration

Figure 5: Plugin returns to normal after permission fix, final permission configuration

⚙️ 5. Cloudflare Dashboard Recommended Configuration Verification


📌 Figure 6: Speed page configuration takes effect

Figure 6: Speed page configuration takes effect

📌 Figure 7: Caching page update time record

Figure 7: Caching page update time record

📌 Figure 8: Security page update record

Figure 8: Security page update record

🧠 Key Conclusions:

  • Apply Recommended actually modifies Zone Settings
  • Speed / Caching / Security are all written to

🚀 6. Auto Purge Content On Update (Key Configuration)


📌 Figure 9: Auto Purge Content On Update enabled

Figure 9: Auto Purge Content On Update enabled

🧠 Purpose:

  • Publish article → auto purge
  • Update article → auto refresh cache
  • Comment → trigger cache update

🧩 7. New Core Rule: HTML Page Caching (SEO-Safe)


📌 Figure 10: New rule added in Cache Rules

Figure 10: New rule added in Cache Rules

🧠 Rule Details:

JavaScript
(not starts_with(http.request.uri.path, "/wp-admin/")
and not starts_with(http.request.uri.path, "/wp-login.php")
and not starts_with(http.request.uri.path, "/wp-json/")
and not starts_with(http.request.uri.path, "/feed/")
and not starts_with(http.request.uri.path, "/xmlrpc.php")
and not http.cookie contains "wordpress_logged_in")

🧠 Rule Explanation:

✔ Cache HTML pages (SEO optimization)


❌ Excluded:

  • Admin dashboard
  • Login page
  • REST API
  • Feed
  • XML-RPC

❗ Key Point:

Plaintext
wordpress_logged_in

Used for:

🧠 Bypassing cache for logged-in users


🧠 8. Final Cache Strategy Closed Loop


📌 Figure 11: cf-cache-status = HIT

Figure 11: cf-cache-status = HIT

📌 Figure 12: Comments take effect in real time

Figure 12: Comments take effect in real time

🔄 9. Complete Cache Flow

Plaintext
User visits page
        ↓
Cloudflare Cache Rules evaluation
        ↓
Logged-in user → bypass
Logged-out user → cache HTML
        ↓
Edge returns HIT
        ↓
WordPress triggers update via purge

🚨 10. Key Pitfalls Summary


❌ 1. Misunderstanding Apply Recommended

Reality:

Does not enable all configurations
Does not modify Cache Rules


❌ 2. Plugin Forbidden Issue

Cause:

  • Incomplete API Token permissions

❌ 3. Mistakenly Believing Cache Rules Can Be Omitted

Reality:

🧠 Cache Rules are the core control layer for the WordPress CDN


🧠 11. Final Architecture Summary

Plaintext
Cloudflare Recommended Configuration (Base Layer)
        ↓
Cache Rules (Business Control Layer)
        ↓
WordPress Auto Purge (Trigger Layer)
        ↓
Edge Cache (Performance Layer)

🎯 12. Final Conclusion


🟢 Cloudflare Recommended Configuration:

✔ Provides baseline optimization
✔ Does not replace Cache Rules


🟢 Cache Rules:

✔ Controls login state / API / HTML caching


🟢 Auto Purge:

✔ Ensures real-time content updates


💡 Final One-Sentence Summary

🧠 The Cloudflare recommended configuration handles the “performance baseline,” Cache Rules handle “business correctness,” and Auto Purge handles “data consistency”

系列导航

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系