| 开发者 | dreamsnet |
|---|---|
| 更新时间 | 2026年6月16日 07:15 |
| PHP版本: | 7.0 及以上 |
| WordPress版本: | 7.0 |
| 版权: | GPLv2 or later |
| 版权网址: | 版权信息 |
ms-cache: excluded HTTP header, which can be used by Varnish to skip caching.https://api.cloudflare.com/client/v4/) to synchronize Varnish cache invalidation with Cloudflare CDN edge cache. This requires a Cloudflare account, a Zone ID, and an API Token with "Zone.Cache Purge" permission. No personal data or visitor information is transmitted — only URL paths and authentication credentials are sent.
Varnish Cache — The plugin opens raw TCP socket connections to a Varnish server at a user-configured IP address and port (default: 127.0.0.1:80). This is typically a localhost connection to infrastructure managed by the site administrator. No external third-party service is contacted for this functionality.
Data Handling & Logging
When debug mode is enabled by the administrator, the plugin logs technical data (URL paths, timestamps, socket connection status, purge results) to files in wp-content/uploads/mscache-logs/. Log files are protected with .htaccess rules, obfuscated filenames, and an index.php guard. No personally identifiable information (PII) or visitor data is collected, stored, or transmitted. Purge statistics (success/failure counts, recent activity) are stored in the WordPress options table and are visible only to administrators.
mscache-varnish-purge folder to /wp-content/plugins/.This plugin has been designed, developed, tested, and validated on the hosting infrastructure of Managed Server S.r.l. — Performance Managed Hosting (managedserver.it). The reference stack consists of Nginx (SSL termination and micro-caching) + Varnish Cache 3.x/4.x (HTTP reverse proxy) + Apache/Nginx (backend) + PHP-FPM, running on Linux. The plugin communicates with Varnish via raw TCP socket on a configurable IP address and port. Whether it works on other hosting environments depends entirely on:
Your VCL must handle PURGE requests from the WordPress server and optionally support the ms-cache: excluded header for cache exclusions. Below is the reference VCL configuration tested and validated on the Managed Server infrastructure, for Varnish 3.x. Replace 123.123.123.123 with your server's public IP address and 127.0.0.2 with your actual backend address and port.
vcl_recv — PURGE handling (single URL + full domain):
`
backend website {
.host = "127.0.0.2";
.port = "80";
}
sub vcl_recv {
Set X-Forwarded-For header
if (req.http.x-forwarded-for) {
set req.http.X-Forwarded-For =
req.http.X-Forwarded-For + ", " + client.ip;
} else {
set req.http.X-Forwarded-For = client.ip;
}
---------------------------------------------------------------
PURGE — Single URL invalidation
The plugin sends: PURGE /path/ HTTP/1.0 with Host header
For full cache purge: PURGE /.* HTTP/1.0
---------------------------------------------------------------
if (req.request == "PURGE") {
if ((req.http.X-Forwarded-For == "123.123.123.123, 127.0.0.1")
|| (req.http.X-Forwarded-For == "127.0.0.1")) {
ban("req.url ~ ^" + req.url + "$"
+ " && req.http.host == " + req.http.host);
error 200 "Purged.";
} else {
error 405 "Not allowed.";
}
}
---------------------------------------------------------------
PURGE with X-Purge-Domain — Full domain invalidation
Invalidates all cached objects for a specific domain
---------------------------------------------------------------
if ((req.request == "PURGE")
&& ((req.http.X-Forwarded-For == "123.123.123.123")
|| (req.http.X-Forwarded-For == "127.0.0.1"))
&& (req.http.X-Purge-Domain)) {
ban("req.http.host == " + req.http.X-Purge-Domain);
error 200 req.http.X-Purge-Domain + " purged.";
}
... your other vcl_recv rules ...
}
`
vcl_fetch — Respect the ms-cache: excluded header from the plugin:
`
sub vcl_fetch {
---------------------------------------------------------------
MSCache exclusion header
When the plugin detects that a page matches an exclusion
pattern (e.g., /cart/, /checkout/, /my-account/*), it adds
the response header: ms-cache: excluded
This block ensures Varnish does NOT cache those responses.
---------------------------------------------------------------
if (beresp.http.ms-cache == "excluded") {
set beresp.ttl = 0s;
set beresp.http.X-Cache = "EXCLUDED";
Optionally hide the header from end users:
unset beresp.http.ms-cache;
return (hit_for_pass);
}
... your other vcl_fetch rules ...
}
`
The plugin sends an HTTP PURGE request to Varnish via a raw TCP socket connection (fsockopen). The request includes the URL path to invalidate and the correct Host header for the domain.
Single URL purge — When a post is saved, the plugin sends:
PURGE /2026/04/03/my-post-title/ HTTP/1.0 Host: www.example.com Connection: Close
The VCL uses ban() with a regex anchored match (^/path/$) to invalidate the URL. This approach is used instead of return(purge) because most production Varnish configurations include additional keys in vcl_hash (e.g., x-ua-device for mobile/desktop variants). A ban() invalidates all variants of the URL in a single operation, regardless of the hash.
Full cache purge — When "Purge Entire Cache" is clicked, the plugin sends:
PURGE /.* HTTP/1.0 Host: www.example.com Connection: Close
The VCL interprets /.* as a regex matching all URLs for that host, effectively invalidating the entire domain cache.
Asynchronous (fire-and-forget) — The socket is closed immediately after sending the request. The plugin does NOT wait for Varnish to respond, so the purge request does not block the WordPress request. In debug mode, the plugin reads the response status line for logging purposes.
return(purge) in VCL) removes a single cached object matching an exact URL and hash. It is fast and frees memory immediately, but only works for the exact hash — if your vcl_hash includes device type, language, or other keys, you would need a separate PURGE for each variant.ban() in VCL) adds a rule to the ban list that retroactively invalidates all cached objects matching a condition. Objects are checked against the ban list when they are next requested. BAN is more flexible because it can match patterns (regex) and invalidates all variants of a URL regardless of the hash.When a frontend request path matches one of the exclusion patterns configured in the plugin settings (e.g., /cart/*, /checkout/*, /my-account/*), the plugin adds the HTTP response header ms-cache: excluded. This header does nothing by itself — it is a signal for your caching layer to skip caching for that response.
In Varnish: The vcl_fetch block shown above checks for this header and sets beresp.ttl = 0s + return(hit_for_pass), ensuring the response is never stored in cache.
In Nginx proxy cache: You can use this header with proxy_no_cache to achieve the same effect. See the Nginx integration section in the plugin documentation.
WooCommerce pages (Cart, Checkout, My Account) can be automatically excluded without manual pattern configuration by enabling the "Auto-Exclude WooCommerce Pages" option in the WooCommerce tab.
Yes. The plugin fully supports WordPress Multisite with a dual-layer configuration:
wp mscache purge-network command purges the cache for all sites in the network in a single operation, iterating through each site with the correct host header.Yes. The plugin only opens outbound TCP connections to the configured Varnish IP (default: 127.0.0.1, localhost). It does not require any special PHP extensions beyond the standard fsockopen function available in all PHP installations. It is compatible with PHP 7.0 through PHP 8.x without deprecation warnings.
The WordPress HTTP API (wp_remote_request) sends the request and waits for the full HTTP response cycle before returning control to WordPress. This adds latency to every post save.
Raw sockets with fsockopen allow the plugin to:
The plugin integrates with:
Many websites use Cloudflare as a CDN and security layer in front of their origin server. When Varnish is purged, the content at the origin is fresh — but Cloudflare edge nodes around the world may still serve stale cached copies to visitors for hours or days. The plugin solves this by optionally sending purge requests to the Cloudflare API v4 every time Varnish is purged. Configuration requires a Cloudflare API Token (with "Zone.Cache Purge" permission) and the Zone ID of your domain, both available from the Cloudflare dashboard. Three purge modes are available:
WP Rocket is one of the most popular WordPress performance plugins, providing page caching, file optimization, and lazy loading. When both MSCache and WP Rocket are active, two separate caching layers exist: Varnish (server-level) and WP Rocket (application-level). If only one is purged, visitors may see inconsistent content. The integration provides bidirectional synchronization:
rocket_clean_post() to clear that page from WP Rocket's local page cache. On a full Varnish purge, rocket_clean_domain() clears the entire WP Rocket cache.after_rocket_clean_domain, after_rocket_clean_post, and after_rocket_clean_home hooks and sends corresponding PURGE requests to Varnish.FlyingPress is a lightweight WordPress performance plugin focused on page caching and CDN. The integration works similarly to WP Rocket:
FlyingPress\Purge::purge_url() for single pages and FlyingPress\Purge::purge_everything() for full cache purge.flying_press_purge_everything action. When FlyingPress clears its cache, MSCache also sends a full PURGE to Varnish.FLYING_PRESS_VERSION constant or class existence and shows the detection status in the Integrations tab.
W3 Total Cache (W3TC) is a comprehensive performance plugin with page cache, database cache, object cache, and CDN support. The integration is one-directional (MSCache → W3TC):
w3tc_flush_post() to clear that page from all W3TC cache layers.w3tc_flush_all() to clear the entire W3TC cache.Autoptimize aggregates and minifies CSS and JavaScript files to reduce HTTP requests and file sizes. After a theme or plugin update, the optimized asset bundles may reference outdated code. The integration is one-directional and limited to full purge only:
autoptimizeCache::clearall() to regenerate all optimized CSS/JS bundles.WordPress object cache (Redis, Memcached, APCu) stores transients, database query results, and rendered fragments in memory. When Varnish is fully purged (e.g., after a major site update), the object cache may still contain stale data that causes WordPress to render pages with outdated content — even though Varnish fetches fresh HTML from the backend. The integration is one-directional and limited to full purge only:
wp_cache_flush() to clear the entire object cache.WooCommerce introduces unique caching challenges: product prices change with sales and coupons, stock quantities change with every order, reviews affect displayed ratings, and pages like Cart, Checkout, and My Account contain user-specific dynamic content that must never be cached. The plugin provides a dedicated WooCommerce tab with granular control over each integration point:
wc_get_page_id() function and marked with the ms-cache: excluded header. This works regardless of custom page slugs or permalink structures, without requiring manual exclusion pattern configuration.