Hello @bcworkz,
Thank you! ?? You’re right. Is not the ideal solution.
Due to the cache plugins (WP Super Cache, W3 Total Cache, etc.), some plugins (the most important ones at least) understand that they should use DONOTCACHEPAGE for more compatibility. This gave me this idea to improve the compatibility of my Varnish with some plugins.
But, it’s just an add-on, in my VCL I have already configured that Varnish should skip some pages. For example:
if (req.url ~ "(?i)/wp-admin|/admin/|/adm/|/login/|/wp-(login|signup|activate|mail|cron)\.php|preview\=true|/xmlrpc\.php|/bb-admin|control\.php|bb-login\.php|bb-reset-password\.php|register\.php") {
if (req.url !~ "(?i)^[^?]*\.(bmp|css|gif|ico|jpeg|jpg|js|png|svg|svgz)(\?.*)?$") {
set req.http.X-BypassVarnish = "allready";
set req.http.X-BackendUncacheable = "admin-page";
return(pass);
}
}
if (req.url ~ "(?i)/(cart/|my-account/|checkout/|addons/|logout/|lost-password/)|(\?add-to-cart=|\?wc-api=)") {
set req.http.X-BypassVarnish = "allready";
set req.http.X-BackendUncacheable = "admin-page";
return (pass);
}
if (req.http.Cookie) {
if (req.http.Cookie ~ "(?i)wordpress_logged_in_|comment_|wp-postpass_") {
set req.http.X-BackendUncacheable = "cookie-session";
set req.http.X-BypassVarnish = "allready";
return(pass);
}
}
This is only for some cases where some people rename the standard WooCommerce page or the WordPress administrative page.
It’s conceivable a plugin simply outputs a no-cache meta tag and does not decide to do so until it’s too late to send headers — that Catch-22.
And you’re right again, If the Header is not sent before, it will not have another chance, but the person can clear the cache manually when this happens or ask me to bypass the cache in VLC, it is not a very serious problem.
Or they may output Cache-Control or Pragma headers only. You can check for these in the “wp_headers” filter, but if they are set by .htaccess rules, all PHP can do is read the file, searching for the appropriate Header set rules. Or does Varnish check for these headers anyway?
By default, Varnish Cache work that way, analyzing the Cache-Control headers.
But to get a higher cache hit rate on Varnish, I’d rather customize my cache completely.
So I get a great saving of resources, minimally compromising the functionality of WordPress.
The final code looks like this:
function donotcache() {
if(defined('DONOTCACHEPAGE') || defined('DOING_CRON') || is_admin() || $GLOBALS['pagenow'] === 'wp-login.php') {
// Do not set header if header has already been sent
if (!headers_sent() && $_SERVER['HTTP_X_SERVER'] == "blizhost-varnish") {
header("BypassVarnishCache: TRUE");if(!$_SERVER['HTTP_X_BYPASSVARNISH']){exit;}
}
}
}
function donotcache_filter($template) {
donotcache();
return $template;
}
add_filter( 'init', donotcache );
add_filter( 'template_include', donotcache_filter );
I really appreciate your help and opinion! ??