BoldGrid / w3-total-cache

GNU General Public License v2.0
152 stars 85 forks source link

Vary X-Forwarded-Proto Header conflicts with Cloudflare WebP #242

Open kallehauge opened 4 years ago

kallehauge commented 4 years ago

Hi,

Cloudflares "Polish image compression" feature do not convert images to WebP when there is a Vary header value that is not just Accept-Encoding


Common Cf-Polished statuses

...

__vary_headerpresent: The origin web server has sent a Vary header with a value other than accept-encoding. If the origin web server is attempting to support WebP, disable WebP at the origin web server and let Polish perform the WebP conversion._

Source: Cloudflare support


But the X-Forwarded-Proto rule for Apache found in PgCache_Environment::rules_core_generate_apache ends up adding X-Forwarded-Proto to the Vary header:

/**
 * Set HTTPS
 */
if ( $config->get_boolean( 'pgcache.cache.ssl' ) ) {
    $rules .= "    RewriteCond %{HTTPS} =on\n";
    $rules .= "    RewriteRule .* - [E=W3TC_SSL:_ssl]\n";
    $rules .= "    RewriteCond %{SERVER_PORT} =443\n";
    $rules .= "    RewriteRule .* - [E=W3TC_SSL:_ssl]\n";
    $rules .= "    RewriteCond %{HTTP:X-Forwarded-Proto} =https [NC]\n";
    $rules .= "    RewriteRule .* - [E=W3TC_SSL:_ssl]\n";
    $env_W3TC_SSL = '%{ENV:W3TC_SSL}';
}

Seeing that W3 Total Cache includes a Cloudflare extension, then the easy solution might be something like the following?

/**
 * Set HTTPS
 */
if ( $config->get_boolean( 'pgcache.cache.ssl' ) ) {
    $rules .= "    RewriteCond %{HTTPS} =on\n";
    $rules .= "    RewriteRule .* - [E=W3TC_SSL:_ssl]\n";
    $rules .= "    RewriteCond %{SERVER_PORT} =443\n";
    $rules .= "    RewriteRule .* - [E=W3TC_SSL:_ssl]\n";

    if ( $this->extension_enabled('cloudflare') ) {
        $rules .= "    RewriteCond %{HTTP:X-Forwarded-Proto} =https [NC]\n";
        $rules .= "    RewriteRule .* - [E=W3TC_SSL:_ssl]\n";
    }

    $env_W3TC_SSL = '%{ENV:W3TC_SSL}';
}

Also; can I really be the only one who have experienced this issue since the Apache rule was introduced in v.0.9.6? Is there a known workaround/setting that I'm just missing?

/ Best regards

maxicus commented 4 years ago

Try to add novary flag to those RewriteCond manually to rules. Something like

RewriteCond %{HTTP:X-Forwarded-Proto} =https [NC,NV]

And let me know if it helps.

kallehauge commented 4 years ago

Hi @maxicus - just letting you know that I have received and seen the suggestion. I'm still in a running dialogue with Cloudflare about their Polish service but isolated the suggestion seems like it did the trick. I just want to be sure that they are satisfied with what W3 Total Cache adds to the header before I forward that communication :)