{"id":1125,"date":"2026-10-27T11:36:25","date_gmt":"2026-10-27T06:06:25","guid":{"rendered":"https:\/\/merahost.org\/blog\/how-to-use-cloudflare-with-litespeed-cache\/"},"modified":"2026-10-27T11:36:25","modified_gmt":"2026-10-27T06:06:25","slug":"how-to-use-cloudflare-with-litespeed-cache","status":"publish","type":"post","link":"https:\/\/merahost.org\/blog\/how-to-use-cloudflare-with-litespeed-cache\/","title":{"rendered":"How to Use Cloudflare with LiteSpeed Cache"},"content":{"rendered":"<p style=\"font-size:16px;line-height:1.7;color:#333\">When architecting high-throughput web applications and mission-critical content platforms, combining a global Anycast Content Delivery Network (CDN) with an enterprise origin web server represents the benchmark of modern web scalability. However, naive deployments that pair Cloudflare&#8217;s edge proxy with LiteSpeed Web Server (LSWS) and the LiteSpeed Cache (LSCache) engine frequently stumble into crippling operational pitfalls\u2014including double-caching pathologies, desynchronized edge purges, corrupted session headers, and broken client IP logging. At <a href=\"https:\/\/merahost.org\">MeraHost<\/a>, deploying thousands of high-concurrency environments on enterprise NVMe infrastructure has demonstrated that achieving sub-50ms global TTFB requires treating Cloudflare and LiteSpeed not as competing cache layers, but as a unified, symbiotically tuned edge-to-core acceleration pipeline.<\/p>\n<p><!-- more --><\/p>\n<h2 style=\"color:#001b41;font-size:26px;font-weight:700;margin-top:36px;margin-bottom:16px\">How to Use Cloudflare with LiteSpeed Cache: The Enterprise Blueprint<\/h2>\n<div class=\"wp-block-group\" style=\"background:#f9f9f9;border-left:4px solid #001b41;padding:16px 20px;margin:20px 0;line-height:1.6;color:#333\">\n<strong>Quick Answer:<\/strong> To use Cloudflare with LiteSpeed Cache seamlessly, delegate edge DDoS mitigation, global DNS, and static asset distribution to Cloudflare, while allowing LiteSpeed Cache to manage granular full-page caching at the origin. Connect LSCache to Cloudflare using a scoped API Token for automated edge purge synchronization, restore real visitor IPs via LiteSpeed&#8217;s UseClientIPInHeader directive, and enforce Full (Strict) SSL with an Origin CA certificate to ensure zero cache desynchronization.\n<\/div>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">LiteSpeed Web Server is engineered from the ground up to outperform traditional Apache and Nginx web servers in dynamic PHP execution, particularly for platforms like WordPress, Magento, and Drupal. LiteSpeed achieves this through native server-level caching (LSCache), communicating directly through shared memory (SHM) segments rather than through external reverse proxy bottlenecks. Meanwhile, Cloudflare operates an Anycast edge spanning over 330 cities across more than 120 countries, terminating TLS handshakes physically close to end-users and buffering static binaries.<\/p>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">When these two powerhouses are misconfigured, severe performance degradation and stale content anomalies arise. If both Cloudflare and LiteSpeed attempt to cache dynamic HTML without an active synchronization channel, an updated e-commerce price or newly published editorial will refresh on the origin server within seconds\u2014yet continue serving stale HTML from Cloudflare&#8217;s edge data centers for hours. Conversely, aggressive edge caching rules that ignore LiteSpeed&#8217;s Vary headers can leak authenticated administrative session bars or private WooCommerce shopping cart states to unauthorized visitors. This guide delivers the architectural roadmap to eliminate these conflicts, configure automated edge purge webhooks, harden TCP\/IP networking, and optimize throughput across the entire delivery chain.<\/p>\n<h2 style=\"color:#001b41;font-size:24px;font-weight:700;margin-top:36px;margin-bottom:16px\">Architectural Topology: Unifying the Edge Proxy and Origin Cache<\/h2>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">To design an optimal Cloudflare LiteSpeed pipeline, we must map the anatomy of an HTTP request as it traverses from the client browser, through the edge Anycast network, across the WAN backbone, and into the LiteSpeed origin kernel. The lifecycle operates through four distinct layers:<\/p>\n<ol style=\"font-size:15px;line-height:1.8;color:#444;margin:16px 0 24px 20px\">\n<li><strong>Edge Ingress &amp; TLS Termination:<\/strong> The client resolves DNS to Cloudflare Anycast IP addresses. Cloudflare terminates TLS 1.3 or HTTP\/3, applies Layer 3\/4 and Layer 7 Web Application Firewall (WAF) filtering, and checks its local edge cache for static assets (images, CSS, JS, fonts).<\/li>\n<li><strong>Origin Forwarding &amp; True IP Preservation:<\/strong> For cache misses or dynamic requests, Cloudflare proxies the request back to the LiteSpeed origin server over an encrypted backhaul tunnel (enforcing TLS 1.3 with Cloudflare Origin CA). Cloudflare attaches the client&#8217;s actual IP address in the <code>CF-Connecting-IP<\/code> and <code>X-Forwarded-For<\/code> HTTP request headers.<\/li>\n<li><strong>Origin Shared-Memory Processing:<\/strong> LiteSpeed Web Server intercepts the ingress request. If the URI is eligible for caching, LiteSpeed queries its Shared Memory (SHM) hash table. On an origin cache HIT, LiteSpeed streams the pre-compressed (gzip\/Brotli) static HTML directly from NVMe storage into the TCP socket, completely bypassing the PHP runtime engine and database.<\/li>\n<li><strong>Asynchronous Tag-Based Purge Synchronization:<\/strong> When content changes (e.g., a post edit, taxonomy change, or stock update), LiteSpeed Cache immediately invalidates the associated cache tags locally. Simultaneously, via background curl workers, LSCache calls Cloudflare&#8217;s REST API (<code>\/client\/v4\/zones\/{zone_id}\/purge_cache<\/code>) to purge either the specific URLs or the entire edge cache zone, guaranteeing immediate global synchronization.<\/li>\n<\/ol>\n<figure class=\"wp-block-table is-style-regular\">\n<table style=\"width:100%;border-collapse:collapse;margin:24px 0;font-size:15px;text-align:left\">\n<thead style=\"background:#001b41;color:#ffffff\">\n<tr>\n<th style=\"padding:12px 16px;border-bottom:2px solid #001b41\">Architecture Metric<\/th>\n<th style=\"padding:12px 16px;border-bottom:2px solid #001b41\">Naive \/ Misconfigured Stack<\/th>\n<th style=\"padding:12px 16px;border-bottom:2px solid #001b41\">Enterprise Tuned Cloudflare + LiteSpeed<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7\">Global Static Asset TTFB<\/td>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7\">180ms &#8211; 450ms (Origin misses)<\/td>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7;color:#20B038;font-weight:600\">12ms &#8211; 35ms (Cloudflare Edge Cache Hit)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7\">Origin Dynamic HTML TTFB<\/td>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7\">380ms &#8211; 1100ms (PHP\/MySQL query loop)<\/td>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7;color:#20B038;font-weight:600\">18ms &#8211; 45ms (LiteSpeed SHM Hit on NVMe)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7\">Cache Purge Synchronization<\/td>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7\">Manual purge or stale 4-hour edge TTL<\/td>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7;color:#20B038;font-weight:600\">Instant Automated Edge Purge via Scoped API<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7\">Client IP Logging &amp; Security<\/td>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7\">Cloudflare reverse-proxy IPs logged (Broken WAF)<\/td>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7;color:#20B038;font-weight:600\">True Visitor IP Restored via UseClientIPInHeader<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7\">SSL\/TLS Cryptographic Security<\/td>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7\">Flexible SSL (Vulnerable to MITM &amp; Redirect Loops)<\/td>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7;color:#20B038;font-weight:600\">Full (Strict) TLS 1.3 with Cloudflare Origin CA<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7\">Origin Concurrency Ceiling<\/td>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7\">~250 concurrent unbuffered requests<\/td>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7;color:#20B038;font-weight:600\">25,000+ concurrent requests (98% Edge\/SHM Offload)<\/td>\n<\/tr>\n<tr>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7\">Dynamic State Isolation<\/td>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7\">Leaked shopping carts via naive Edge Caching<\/td>\n<td style=\"padding:12px 16px;border-bottom:1px solid #e7e7e7;color:#20B038;font-weight:600\">Strict Cookie\/Vary Isolation via LSCache Tags<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<blockquote class=\"wp-block-quote\" style=\"background:#f9f9f9;border-left:4px solid #001b41;padding:16px 20px;margin:24px 0\">\n<p><strong style=\"color:#001b41\">Architecture Note:<\/strong> Avoid Cloudflare&#8217;s blanket &#8220;Cache Everything&#8221; Page Rule unless you are running Cloudflare Automatic Platform Optimization (APO) or custom edge Cloudflare Workers. Without proper worker logic, &#8220;Cache Everything&#8221; will indiscriminately cache dynamic HTML responses containing user session cookies, displaying logged-in customer dashboards, WooCommerce cart fragments, or WordPress administrative controls to other visitors.<\/p>\n<\/blockquote>\n<h2 style=\"color:#001b41;font-size:24px;font-weight:700;margin-top:36px;margin-bottom:16px\">Step 1: Restoring Real Visitor IPs in LiteSpeed Web Server<\/h2>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">Because Cloudflare operates as a reverse proxy, every incoming connection hitting your LiteSpeed origin server originates from Cloudflare&#8217;s Anycast IP addresses. If your origin web server is not configured to extract the real visitor IP from the <code>CF-Connecting-IP<\/code> request header, three catastrophic problems occur:<\/p>\n<ul style=\"font-size:15px;line-height:1.8;color:#444;margin:16px 0 24px 20px\">\n<li><strong>Security Failures:<\/strong> Origin firewalls (such as ConfigServer Security &amp; Firewall &#8211; CSF, Fail2ban, or LiteSpeed Per-Client Throttling) will mistakenly ban Cloudflare&#8217;s edge proxy IPs rather than the offending attacker, taking your entire website offline for everyone routed through that edge PoP.<\/li>\n<li><strong>Broken Analytics &amp; Geo-Targeting:<\/strong> WordPress comment logging, WooCommerce fraud scoring, and geo-currency selectors will read Cloudflare&#8217;s data center locations instead of the end-user&#8217;s geographic origin.<\/li>\n<li><strong>Vary Header Desynchronization:<\/strong> LiteSpeed Cache&#8217;s geo-caching and mobile-view caching engines will fail to identify visitor variations accurately.<\/li>\n<\/ul>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">To resolve this natively inside LiteSpeed Web Server, configure the <code>useIpInProxyHeader<\/code> directive within your LiteSpeed configuration console or directly in <code>\/usr\/local\/lsws\/conf\/httpd_config.conf<\/code>. When set to <code>2<\/code> (or <code>Trusted IP Only<\/code>), LiteSpeed inspects the proxy header only if the connecting socket originates from a trusted upstream IP address.<\/p>\n<p style=\"font-size:14px;font-weight:600;color:#001b41;margin-top:20px;margin-bottom:8px\">LiteSpeed Web Server Origin IP Configuration (<code>\/usr\/local\/lsws\/conf\/httpd_config.conf<\/code>):<\/p>\n<pre class=\"wp-block-code\" style=\"background:#f3f3f3;color:#333;padding:16px;border-left:4px solid #001b41;font-family:monospace;font-size:13px\"><code># Enable Client IP extraction from Cloudflare proxy headers\nuseIpInProxyHeader         2\n\n# Trust all official Cloudflare IPv4 and IPv6 Anycast ranges\n# Ensure accessControl allow list permits Cloudflare edge subnets\naccessControl  {\n  allow                   ALL, 173.245.48.0\/20T, 103.21.244.0\/22T, 103.22.200.0\/22T, 103.31.4.0\/22T, 141.101.64.0\/18T, 108.162.192.0\/18T, 190.93.240.0\/20T, 188.114.96.0\/20T, 197.234.240.0\/22T, 198.41.128.0\/17T, 162.158.0.0\/15T, 104.16.0.0\/13T, 104.24.0.0\/14T, 172.64.0.0\/13T, 131.0.72.0\/22T, 2400:cb00::\/32T, 2606:4700::\/32T, 2803:f800::\/32T, 2405:b500::\/32T, 2405:8100::\/32T, 2a06:98c0::\/29T, 2c0f:f248::\/32T\n}<\/code><\/pre>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">If you are operating OpenLiteSpeed (OLS) or utilizing an Apache-compatible configuration file with <code>mod_remoteip<\/code>, apply the equivalent directive inside your virtual host container or Apache include configuration:<\/p>\n<p style=\"font-size:14px;font-weight:600;color:#001b41;margin-top:20px;margin-bottom:8px\">Apache \/ OpenLiteSpeed RemoteIP Configuration (<code>\/etc\/apache2\/conf-available\/remoteip.conf<\/code>):<\/p>\n<pre class=\"wp-block-code\" style=\"background:#f3f3f3;color:#333;padding:16px;border-left:4px solid #001b41;font-family:monospace;font-size:13px\"><code># Define the real IP header injected by Cloudflare\nRemoteIPHeader CF-Connecting-IP\n\n# Trusted Cloudflare IPv4 CIDR blocks\nRemoteIPTrustedProxy 173.245.48.0\/20\nRemoteIPTrustedProxy 103.21.244.0\/22\nRemoteIPTrustedProxy 103.22.200.0\/22\nRemoteIPTrustedProxy 103.31.4.0\/22\nRemoteIPTrustedProxy 141.101.64.0\/18\nRemoteIPTrustedProxy 108.162.192.0\/18\nRemoteIPTrustedProxy 190.93.240.0\/20\nRemoteIPTrustedProxy 188.114.96.0\/20\nRemoteIPTrustedProxy 197.234.240.0\/22\nRemoteIPTrustedProxy 198.41.128.0\/17\nRemoteIPTrustedProxy 162.158.0.0\/15\nRemoteIPTrustedProxy 104.16.0.0\/13\nRemoteIPTrustedProxy 104.24.0.0\/14\nRemoteIPTrustedProxy 172.64.0.0\/13\nRemoteIPTrustedProxy 131.0.72.0\/22\n\n# Trusted Cloudflare IPv6 CIDR blocks\nRemoteIPTrustedProxy 2400:cb00::\/32\nRemoteIPTrustedProxy 2606:4700::\/32\nRemoteIPTrustedProxy 2803:f800::\/32\nRemoteIPTrustedProxy 2405:b500::\/32\nRemoteIPTrustedProxy 2405:8100::\/32\nRemoteIPTrustedProxy 2a06:98c0::\/29\nRemoteIPTrustedProxy 2c0f:f248::\/32<\/code><\/pre>\n<h2 style=\"color:#001b41;font-size:24px;font-weight:700;margin-top:36px;margin-bottom:16px\">Step 2: Automating Cloudflare Edge Purging in LiteSpeed Cache<\/h2>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">The most critical integration point between LiteSpeed and Cloudflare is automated edge cache invalidation. LiteSpeed Cache features native Cloudflare API hooks. When an author publishes a new article, edits a WooCommerce product inventory count, or deletes a spam comment, LiteSpeed invalidates its local origin shared-memory cache immediately. By supplying your Cloudflare API credentials into the LiteSpeed Cache settings, LSCache sends an instant REST call to Cloudflare to purge the edge simultaneously.<\/p>\n<ol style=\"font-size:15px;line-height:1.8;color:#444;margin:16px 0 24px 20px\">\n<li><strong>Generate a Scoped Cloudflare API Token:<\/strong> Log into the Cloudflare Dashboard, navigate to <em>My Profile &gt; API Tokens<\/em>, and click <em>Create Token<\/em>. Do NOT use your global API key. Select the <em>Custom Token<\/em> template and grant the following permissions:\n<ul style=\"margin-top:8px\">\n<li><code>Zone &gt; Cache Purge &gt; Purge<\/code><\/li>\n<li><code>Zone &gt; Zone &gt; Read<\/code><\/li>\n<\/ul>\n<p>    Restrict the Zone Resources to your specific domain to adhere to the principle of least privilege.\n  <\/li>\n<li><strong>Locate Zone ID:<\/strong> In the Cloudflare overview tab for your domain, scroll down to the bottom right sidebar under the <em>API<\/em> heading to copy your 32-character <em>Zone ID<\/em>.<\/li>\n<li><strong>Configure LiteSpeed Cache Plugin:<\/strong> Inside your WordPress admin dashboard, navigate to <em>LiteSpeed Cache &gt; CDN &gt; Cloudflare Settings<\/em>:\n<ul style=\"margin-top:8px\">\n<li><strong>Cloudflare API:<\/strong> Set toggle to <code>ON<\/code>.<\/li>\n<li><strong>API Token \/ Global Key:<\/strong> Enter your Scoped Cloudflare API Token.<\/li>\n<li><strong>Email:<\/strong> Enter your Cloudflare account email address (if using API Token, this can be omitted depending on plugin version).<\/li>\n<li><strong>Domain:<\/strong> Enter your apex domain (e.g., <code>example.com<\/code>).<\/li>\n<li><strong>Zone ID:<\/strong> Paste the 32-character Zone ID copied from Cloudflare.<\/li>\n<\/ul>\n<\/li>\n<li><strong>Enable Auto-Purge Hooks:<\/strong> Under <em>LiteSpeed Cache &gt; Purge<\/em>, ensure that automatic purge hooks are active for Front Page, Home Page, Pages, All Categories, and Post Tags upon publishing.<\/li>\n<\/ol>\n<blockquote class=\"wp-block-quote\" style=\"background:#f9f9f9;border-left:4px solid #001b41;padding:16px 20px;margin:24px 0\">\n<p><strong style=\"color:#001b41\">Architecture Note:<\/strong> When managing high-traffic editorial portals, avoid triggering a full &#8220;Purge All&#8221; on every minor post update. Full zone purges flush all static assets globally, forcing millions of subsequent requests back to your origin server simultaneously (a thundering herd condition). LiteSpeed Cache&#8217;s granular URL and tag-based purges selectively clear only modified resources, keeping your origin CPU load near zero.<\/p>\n<\/blockquote>\n<h2 style=\"color:#001b41;font-size:24px;font-weight:700;margin-top:36px;margin-bottom:16px\">Step 3: Linux Kernel &amp; TCP Socket Tuning for Ingress Edge Traffic<\/h2>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">When thousands of Cloudflare edge nodes establish concurrent TCP connections to your LiteSpeed origin server, default Linux kernel network parameters quickly become saturated. In a default Linux deployment, connection backlogs are limited to 128 or 1024 slots, causing socket drops, SYN flood false alarms, and high connection latency. To guarantee flawless edge-to-origin data transit, deploy this hardened sysctl profile to <code>\/etc\/sysctl.d\/99-cloudflare-litespeed.conf<\/code>.<\/p>\n<p style=\"font-size:14px;font-weight:600;color:#001b41;margin-top:20px;margin-bottom:8px\">Production Linux Kernel Network Optimization (<code>\/etc\/sysctl.d\/99-cloudflare-litespeed.conf<\/code>):<\/p>\n<pre class=\"wp-block-code\" style=\"background:#f3f3f3;color:#333;padding:16px;border-left:4px solid #001b41;font-family:monospace;font-size:13px\"><code># Enable Google BBR Congestion Control for high-throughput edge backhaul\nnet.core.default_qdisc = fq\nnet.ipv4.tcp_congestion_control = bbr\n\n# Increase socket listen backlog to absorb Cloudflare burst connections\nnet.core.somaxconn = 65535\nnet.ipv4.tcp_max_syn_backlog = 65535\nnet.core.netdev_max_backlog = 16384\n\n# Expand ephemeral port range for massive outbound API purges and proxies\nnet.ipv4.ip_local_port_range = 10240 65535\n\n# Enable TCP SYN Cookies for Layer 4 SYN flood resilience\nnet.ipv4.tcp_syncookies = 1\n\n# Optimize TCP socket reuse and teardown latency\nnet.ipv4.tcp_tw_reuse = 1\nnet.ipv4.tcp_fin_timeout = 15\n\n# Enable TCP Fast Open (TFO) for client-to-server and proxy-to-origin handshakes\n# 3 enables both incoming and outgoing TFO\nnet.ipv4.tcp_fastopen = 3\n\n# Buffer tuning: Allocate sufficient memory for high-bandwidth WAN connections\nnet.core.rmem_max = 16777216\nnet.core.wmem_max = 16777216\nnet.ipv4.tcp_rmem = 4096 87380 16777216\nnet.ipv4.tcp_wmem = 4096 65536 16777216\n\n# Disable slow start after idle to maintain high transfer rates across keep-alive connections\nnet.ipv4.tcp_slow_start_after_idle = 0\n\n# Maximum number of orphaned (closed) TCP connections allowed\nnet.ipv4.tcp_max_orphans = 262144<\/code><\/pre>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">After creating the file, apply the network modifications immediately without rebooting the server using the sysctl command:<\/p>\n<pre class=\"wp-block-code\" style=\"background:#f3f3f3;color:#333;padding:16px;border-left:4px solid #001b41;font-family:monospace;font-size:13px\"><code>sysctl --system<\/code><\/pre>\n<h2 style=\"color:#001b41;font-size:24px;font-weight:700;margin-top:36px;margin-bottom:16px\">Step 4: Automated Synchronization of Cloudflare IP Subnets<\/h2>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">Cloudflare occasionally modifies, expands, or reclaims IP prefixes from its Anycast routing pool. Hardcoding static IP lists into your firewall or web server configuration introduces maintenance risk. If Cloudflare routes traffic through a newly announced subnet that is missing from your trusted list, visitors routed through that PoP will either be blocked by your firewall or have their real IPs logged as Cloudflare proxy IPs.<\/p>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">Deploy this automated Bash maintenance script and systemd timer to pull official Cloudflare IP ranges from their published endpoints, rebuild an ipset allowlist, update LiteSpeed&#8217;s trusted IP configuration, and gracefully reload the web server without dropping a single active transaction.<\/p>\n<p style=\"font-size:14px;font-weight:600;color:#001b41;margin-top:20px;margin-bottom:8px\">Production IP Sync Script (<code>\/usr\/local\/bin\/sync-cloudflare-ips.sh<\/code>):<\/p>\n<pre class=\"wp-block-code\" style=\"background:#f3f3f3;color:#333;padding:16px;border-left:4px solid #001b41;font-family:monospace;font-size:13px\"><code>#!\/usr\/bin\/env bash\n# ==============================================================================\n# Enterprise Cloudflare IP Synchronizer for LiteSpeed Web Server\n# Fetches dynamic Anycast subnets and synchronizes iptables \/ ipset &amp; LiteSpeed\n# ==============================================================================\nset -euo pipefail\n\nCLOUDFLARE_V4_URL=\"https:\/\/www.cloudflare.com\/ips-v4\"\nCLOUDFLARE_V6_URL=\"https:\/\/www.cloudflare.com\/ips-v6\"\nTARGET_CONF=\"\/usr\/local\/lsws\/conf\/cloudflare_ips.conf\"\nTMP_DIR=$(mktemp -d \/tmp\/cf_sync.XXXXXX)\n\ncleanup() {\n  rm -rf \"${TMP_DIR}\"\n}\ntrap cleanup EXIT\n\necho \"[+] Fetching current Cloudflare IPv4 and IPv6 subnets...\"\ncurl -fsSL --retry 3 --connect-timeout 10 \"${CLOUDFLARE_V4_URL}\" -o \"${TMP_DIR}\/ips-v4\"\ncurl -fsSL --retry 3 --connect-timeout 10 \"${CLOUDFLARE_V6_URL}\" -o \"${TMP_DIR}\/ips-v6\"\n\n# Verify fetched payloads contain valid IP ranges\nif [[ ! -s \"${TMP_DIR}\/ips-v4\" ]] || [[ ! -s \"${TMP_DIR}\/ips-v6\" ]]; then\n  echo \"[-] ERROR: Empty payload received from Cloudflare API. Aborting.\"\n  exit 1\nfi\n\necho \"[+] Generating LiteSpeed accessControl trusted subnet directive...\"\nprintf 'accessControl  {\\n  allow                   ALL, ' &gt; \"${TMP_DIR}\/cloudflare_ips.conf\"\nsed 's\/$\/T, \/' \"${TMP_DIR}\/ips-v4\" | tr -d '\\n' &gt;&gt; \"${TMP_DIR}\/cloudflare_ips.conf\"\nsed 's\/$\/T, \/' \"${TMP_DIR}\/ips-v6\" | tr -d '\\n' | sed 's\/, $\/\/' &gt;&gt; \"${TMP_DIR}\/cloudflare_ips.conf\"\nprintf '\\n}\\n' &gt;&gt; \"${TMP_DIR}\/cloudflare_ips.conf\"\n\n# Atomically replace production configuration if content differs\nif ! cmp -s \"${TMP_DIR}\/cloudflare_ips.conf\" \"${TARGET_CONF}\" 2&gt;\/dev\/null; then\n  mv \"${TMP_DIR}\/cloudflare_ips.conf\" \"${TARGET_CONF}\"\n  chmod 644 \"${TARGET_CONF}\"\n  echo \"[+] Configuration updated. Performing graceful LiteSpeed reload...\"\n  systemctl reload lsws || \/usr\/local\/lsws\/bin\/lswsctrl restart\nelse\n  echo \"[*] Cloudflare subnets unchanged. No reload required.\"\nfi\n\n# Synchronize IPSet for kernel-level firewall bypass\nif command -v ipset &gt;\/dev\/null 2&gt;&amp;1; then\n  echo \"[+] Synchronizing kernel IPSet 'cloudflare-v4'...\"\n  ipset create cloudflare-v4 hash:net family inet -exist\n  ipset create cf-temp-v4 hash:net family inet -exist\n  while read -r cidr; do\n    [[ -n \"${cidr}\" ]] &amp;&amp; ipset add cf-temp-v4 \"${cidr}\" -exist\n  done &lt; \"${TMP_DIR}\/ips-v4\"\n  ipset swap cf-temp-v4 cloudflare-v4\n  ipset destroy cf-temp-v4\nfi\n\necho \"[\u2713] Cloudflare IP synchronization completed successfully.\"<\/code><\/pre>\n<p style=\"font-size:14px;font-weight:600;color:#001b41;margin-top:20px;margin-bottom:8px\">Systemd Service Unit (<code>\/etc\/systemd\/system\/cloudflare-ip-sync.service<\/code>):<\/p>\n<pre class=\"wp-block-code\" style=\"background:#f3f3f3;color:#333;padding:16px;border-left:4px solid #001b41;font-family:monospace;font-size:13px\"><code>[Unit]\nDescription=Automated Cloudflare IP Synchronizer for LiteSpeed\nAfter=network-online.target\nWants=network-online.target\n\n[Service]\nType=oneshot\nExecStart=\/usr\/local\/bin\/sync-cloudflare-ips.sh\nStandardOutput=journal\nStandardError=journal\n\n[Install]\nWantedBy=multi-user.target<\/code><\/pre>\n<p style=\"font-size:14px;font-weight:600;color:#001b41;margin-top:20px;margin-bottom:8px\">Systemd Timer Unit (<code>\/etc\/systemd\/system\/cloudflare-ip-sync.timer<\/code>):<\/p>\n<pre class=\"wp-block-code\" style=\"background:#f3f3f3;color:#333;padding:16px;border-left:4px solid #001b41;font-family:monospace;font-size:13px\"><code>[Unit]\nDescription=Weekly Cloudflare Subnet Sync Timer\n\n[Timer]\nOnCalendar=Sun *-*-* 03:00:00\nRandomizedDelaySec=1800\nPersistent=true\n\n[Install]\nWantedBy=timers.target<\/code><\/pre>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">Enable and start the systemd timer using standard systemctl commands:<\/p>\n<pre class=\"wp-block-code\" style=\"background:#f3f3f3;color:#333;padding:16px;border-left:4px solid #001b41;font-family:monospace;font-size:13px\"><code>chmod +x \/usr\/local\/bin\/sync-cloudflare-ips.sh\nsystemctl daemon-reload\nsystemctl enable --now cloudflare-ip-sync.timer<\/code><\/pre>\n<h2 style=\"color:#001b41;font-size:24px;font-weight:700;margin-top:36px;margin-bottom:16px\">Step 5: Cloudflare Edge Optimization Rules for LiteSpeed<\/h2>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">Within your Cloudflare Dashboard, configure these specific settings to complement LiteSpeed&#8217;s native caching behavior rather than interfering with it:<\/p>\n<ul style=\"font-size:15px;line-height:1.8;color:#444;margin:16px 0 24px 20px\">\n<li><strong>Caching Level:<\/strong> Set to <strong>Standard<\/strong> under <em>Caching &gt; Configuration<\/em>. This tells Cloudflare to cache all static asset extensions (.css, .js, .webp, .jpg, .svg, .woff2) according to origin headers while letting dynamic HTML requests pass directly through to LiteSpeed.<\/li>\n<li><strong>Browser Cache TTL:<\/strong> Set to <strong>Respect Existing Headers<\/strong>. LiteSpeed Cache outputs optimized <code>Cache-Control<\/code> max-age headers for both static and dynamic assets. Respecting existing headers ensures Cloudflare does not override LiteSpeed&#8217;s intentional cache lifetimes.<\/li>\n<li><strong>Tiered Cache &amp; Argo:<\/strong> Enable <strong>Tiered Cache<\/strong> (Smart Tiered Cache Topology). Cloudflare&#8217;s regional data centers will elect specialized upper-tier hubs to fetch assets from your origin server once, distributing cached copies to all surrounding lower-tier edge PoPs and drastically slashing origin bandwidth consumption.<\/li>\n<li><strong>SSL\/TLS Encryption Mode:<\/strong> Set strictly to <strong>Full (Strict)<\/strong>. Install a free 15-year Cloudflare Origin CA certificate inside your LiteSpeed virtual host. Never use Flexible SSL; Flexible SSL transmits unencrypted HTTP between Cloudflare and your origin server, exposing your credentials over public backhaul lines and triggering infinite redirect loops if HTTPS rewrites are enabled.<\/li>\n<li><strong>Minification Settings:<\/strong> Disable Cloudflare&#8217;s Auto-Minify (HTML, CSS, JS) if you have already enabled CSS\/JS Minification, Combining, and Critical CSS generation inside the LiteSpeed Cache plugin. Running dual minification engines consumes CPU cycles and often introduces JavaScript parse errors due to redundant variable renaming.<\/li>\n<\/ul>\n<blockquote class=\"wp-block-quote\" style=\"background:#f9f9f9;border-left:4px solid #001b41;padding:16px 20px;margin:24px 0\">\n<p><strong style=\"color:#001b41\">Architecture Note:<\/strong> When conducting speed tests on webpagetest.org or Google PageSpeed Insights, always check the HTTP response headers. A perfectly configured Cloudflare LiteSpeed stack will return <code>CF-Cache-Status: HIT<\/code> for static assets, and <code>CF-Cache-Status: DYNAMIC<\/code> alongside <code>X-LiteSpeed-Cache: hit<\/code> for uncached edge HTML requests that hit LiteSpeed&#8217;s shared-memory cache.<\/p>\n<\/blockquote>\n<h2 style=\"color:#001b41;font-size:24px;font-weight:700;margin-top:36px;margin-bottom:16px\">Production Readiness &amp; Performance Verification Checklist<\/h2>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">Before promoting your Cloudflare LiteSpeed stack to live production traffic, verify each layer with this diagnostic curl command executed from an external terminal:<\/p>\n<pre class=\"wp-block-code\" style=\"background:#f3f3f3;color:#333;padding:16px;border-left:4px solid #001b41;font-family:monospace;font-size:13px\"><code># Test dynamic page caching and header preservation\ncurl -I -s https:\/\/example.com\/ | grep -Ei \"(cf-cache-status|x-litespeed-cache|content-encoding|alt-svc)\"\n\n# Expected Output on second request:\n# cf-cache-status: DYNAMIC\n# x-litespeed-cache: hit\n# content-encoding: br\n# alt-svc: h3=\":443\"; ma=86400<\/code><\/pre>\n<p style=\"font-size:15px;line-height:1.7;color:#444\">For engineering teams seeking ultra-reliable cloud hosting without having to manually manage low-level kernel configurations, deploying on <a href=\"https:\/\/merahost.org\">MeraHost Enterprise Cloud<\/a> provides an out-of-the-box, fully managed LiteSpeed Web Server environment running on enterprise NVMe storage arrays with native Cloudflare edge compatibility pre-tuned.<\/p>\n<h2 style=\"color:#001b41;font-size:24px;font-weight:700;margin-top:36px;margin-bottom:16px\">Frequently Asked Questions<\/h2>\n<details class=\"wp-block-group\" style=\"background:#f9f9f9;border:1px solid #e7e7e7;border-radius:4px;padding:14px;margin-bottom:12px\">\n<summary style=\"cursor:pointer;font-weight:600;color:#001b41\">Does Cloudflare replace the need for LiteSpeed Cache at the origin?<\/summary>\n<p style=\"margin-top:10px;color:#444\">No. Cloudflare excels at edge asset distribution, DDoS mitigation, and edge TLS termination, but by default it does not cache dynamic HTML pages. When a user requests an HTML page, Cloudflare forwards the request to your origin server. LiteSpeed Cache executes at the origin, serving pre-rendered pages directly from shared memory in under 20ms without invoking PHP or MySQL. Without LiteSpeed Cache, every edge cache-miss will force your origin server to execute costly database queries.<\/p>\n<\/details>\n<details class=\"wp-block-group\" style=\"background:#f9f9f9;border:1px solid #e7e7e7;border-radius:4px;padding:14px;margin-bottom:12px\">\n<summary style=\"cursor:pointer;font-weight:600;color:#001b41\">Why do I see Cloudflare IP addresses in WordPress comment logs and security alerts?<\/summary>\n<p style=\"margin-top:10px;color:#444\">This occurs because LiteSpeed Web Server is not configured to trust Cloudflare as a reverse proxy. To fix this, enable <code>useIpInProxyHeader 2<\/code> in LiteSpeed&#8217;s <code>httpd_config.conf<\/code> or configure <code>RemoteIPHeader CF-Connecting-IP<\/code> with Cloudflare&#8217;s trusted IP ranges. Once configured, LiteSpeed replaces Cloudflare&#8217;s proxy IP with the real visitor IP address before passing the request to PHP and WordPress.<\/p>\n<\/details>\n<details class=\"wp-block-group\" style=\"background:#f9f9f9;border:1px solid #e7e7e7;border-radius:4px;padding:14px;margin-bottom:12px\">\n<summary style=\"cursor:pointer;font-weight:600;color:#001b41\">Can I use Cloudflare APO (Automatic Platform Optimization) with LiteSpeed Cache?<\/summary>\n<p style=\"margin-top:10px;color:#444\">While technically possible, running Cloudflare APO alongside LiteSpeed Cache is generally redundant and can introduce cache invalidation race conditions. If you choose to use Cloudflare APO, you must ensure that LiteSpeed Cache handles object caching and CSS\/JS optimization while delegating page caching to APO. For most enterprise websites, using LiteSpeed Cache for page caching combined with Cloudflare&#8217;s Standard edge caching and automated API purges provides superior flexibility, tag-based purging, and granular cache controls.<\/p>\n<\/details>\n<details class=\"wp-block-group\" style=\"background:#f9f9f9;border:1px solid #e7e7e7;border-radius:4px;padding:14px;margin-bottom:12px\">\n<summary style=\"cursor:pointer;font-weight:600;color:#001b41\">What happens when I purge the cache in the LiteSpeed Cache WordPress plugin?<\/summary>\n<p style=\"margin-top:10px;color:#444\">When configured with a scoped Cloudflare API token, purging a page or category in LiteSpeed Cache immediately clears the corresponding entry from LiteSpeed&#8217;s origin shared memory and simultaneously dispatches an asynchronous API call to Cloudflare. Cloudflare purges that specific URL from its global edge data centers within seconds, ensuring visitors worldwide immediately receive the fresh content without stale caching delays.<\/p>\n<\/details>\n<div class=\"wp-block-group has-background\" style=\"background:#f9f9f9;border:1px solid #e7e7e7;border-radius:8px;padding:32px;margin:40px 0;text-align:center\">\n<h3 style=\"color:#001b41;margin-top:0;font-size:24px;font-weight:700\">Deploy Enterprise-Grade Production Infrastructure<\/h3>\n<p style=\"color:#444;font-size:16px;line-height:1.6;max-width:680px;margin:12px auto 24px auto\">Need guaranteed performance with zero price hikes? Host mission-critical workloads on <strong style=\"color:#001b41\">MeraHost<\/strong> with pure Enterprise NVMe, LiteSpeed Web Server, and Same Renewal Price, Always (starting at \u20b999\/mo).<\/p>\n<div class=\"wp-block-buttons\" style=\"display:flex;gap:16px;justify-content:center;flex-wrap:wrap\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link\" href=\"https:\/\/merahost.org\" style=\"background:#001b41;color:#ffffff;font-weight:700;padding:12px 28px;border-radius:4px;text-decoration:none;display:inline-block;font-size:15px\">Explore MeraHost NVMe Cloud &rarr;<\/a><\/div>\n<div class=\"wp-block-button is-style-outline\"><a class=\"wp-block-button__link\" href=\"https:\/\/cpanelfree.com\" style=\"background:transparent;color:#001b41;font-weight:600;padding:12px 24px;border:2px solid #001b41;border-radius:4px;text-decoration:none;display:inline-block;font-size:15px\" rel=\"nofollow noopener\" target=\"_blank\">Deploy Free Staging on CpanelFree<\/a><\/div>\n<\/div>\n<\/div>\n\n\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-bottom\"\n    data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1125&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;bottom&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\\\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;20&quot;,&quot;title&quot;:&quot;How to Use Cloudflare with LiteSpeed Cache&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n            \n<div class=\"kksr-stars\">\n    \n<div class=\"kksr-stars-inactive\">\n            <div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 20px; height: 20px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 20px; height: 20px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 20px; height: 20px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 20px; height: 20px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 20px; height: 20px;\"><\/div>\n        <\/div>\n    <\/div>\n    \n<div class=\"kksr-stars-active\" style=\"width: 0px;\">\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 20px; height: 20px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 20px; height: 20px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 20px; height: 20px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 20px; height: 20px;\"><\/div>\n        <\/div>\n            <div class=\"kksr-star\" style=\"padding-right: 5px\">\n            \n\n<div class=\"kksr-icon\" style=\"width: 20px; height: 20px;\"><\/div>\n        <\/div>\n    <\/div>\n<\/div>\n                \n\n<div class=\"kksr-legend\" style=\"font-size: 16px;\">\n            <span class=\"kksr-muted\">Rate this post<\/span>\n    <\/div>\n    <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Master Cloudflare with LiteSpeed Cache for enterprise speed. Eliminate cache synchronization conflicts and optimize edge purging with this architectural guide.<\/p>\n","protected":false},"author":1,"featured_media":1124,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[135],"tags":[126,125,136,129,127],"class_list":["post-1125","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-optimization","tag-devops","tag-linux","tag-optimization","tag-performance","tag-sysadmin"],"_links":{"self":[{"href":"https:\/\/merahost.org\/blog\/wp-json\/wp\/v2\/posts\/1125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/merahost.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/merahost.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/merahost.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/merahost.org\/blog\/wp-json\/wp\/v2\/comments?post=1125"}],"version-history":[{"count":0,"href":"https:\/\/merahost.org\/blog\/wp-json\/wp\/v2\/posts\/1125\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/merahost.org\/blog\/wp-json\/wp\/v2\/media\/1124"}],"wp:attachment":[{"href":"https:\/\/merahost.org\/blog\/wp-json\/wp\/v2\/media?parent=1125"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/merahost.org\/blog\/wp-json\/wp\/v2\/categories?post=1125"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/merahost.org\/blog\/wp-json\/wp\/v2\/tags?post=1125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}