Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #33202
    Nicola
    Guest

    Hello,

    I am using nginx and w3tc minify with disk cache writes correctly to disk both gzip and brotli versions of the minified files.
    Most modern browsers will ask for both “gzip,br”, but when serving from disk cache (on cache hit) w3tc serves the larger gzip version. This is caused by the order of the “if” statements in nginx.conf, where the second match (gzip) overwrites the first one (br).

    It should be possible to serve brotli as a preference by swapping the two “if” statements in Minify_Environment.php (around line 459).
    Please let me know if this makes sense, right now I patched manually the file.

    Regards,
    Nicola

    #33218
    Marko Vasiljevic
    Keymaster

    Hello Nicola,

    Thank you for your inquiry and I am happy to assist you with this.
    Browsers that support brotli send br along with gzip in the accept-encoding request header. If brotli is enabled on your web server, you will get a response in brotli compressed format.
    This means to me that if the client supports both, brotli is preferred over gzip.
    At the moment I can see that all minified files are “brotlied”
    If convenient, can you please share the exact changes you made in Minify_Environment.php and also, are both brotli and gzip enabled in Performance>Browser Cache>CSS&JS and if so, you should disable gzip if you are using brotli for those files.
    Thank you!

    #33235
    Nicola
    Guest

    Hi Marko,

    > If brotli is enabled on your web server, you will get a response in brotli compressed format.

    This is correct for content compressed by nginx, but on my installation this doesn’t happen for the minify cache folder on disk. This is because w3tc’s nginx.conf in the minify directory disables nginx compression (gzip off / brotli off) and it serves a raw pre-compressed file, written by PHP, by appending _gzip or _br to the file name. The priority appears to be set by nginx.conf

    > are both brotli and gzip enabled in Performance>Browser Cache>CSS&JS

    Yes

    > and if so, you should disable gzip if you are using brotli for those files.

    That is an option, but it would be better to keep both gzip and brotli enabled, so older browsers will still get a gzip version if they don’t support brotli.

    The original portion of Minify_Environment.php is:

    459 if ( $brotli ) {
    460 $rules .= “if (\$http_accept_encoding ~ br) {\n”;
    461 $rules .= ” set \$w3tc_enc _br;\n”;
    462 $rules .= “}\n”;
    463 }
    464
    465 if ( $compression ) {
    466 $rules .= “if (\$http_accept_encoding ~ gzip) {\n”;
    467 $rules .= ” set \$w3tc_enc _gzip;\n”;
    468 $rules .= “}\n”;
    469 }
    470

    I changed this on my production server to:

    459 if ( $compression ) {
    460 $rules .= “if (\$http_accept_encoding ~ gzip) {\n”;
    461 $rules .= ” set \$w3tc_enc _gzip;\n”;
    462 $rules .= “}\n”;
    463 }
    474
    455 if ( $brotli ) {
    466 $rules .= “if (\$http_accept_encoding ~ br) {\n”;
    467 $rules .= ” set \$w3tc_enc _br;\n”;
    468 $rules .= “}\n”;
    469 }
    470

    since apparently in the original version $w3tc_enc is set to “_br” on line 461, then overwritten with “_gzip” on line 467 – once they are written out to nginx.conf and parsed by nginx.

    Please note that my main domain is served by a CDN and this complicates external analysis. I can provide you curl samples to my staging environment (original code) and production origin server (patched) – eventually in private.

    #33256
    Marko Vasiljevic
    Keymaster

    Hello Nicola,

    Thank you for providing the info and detailed explanation.
    Let me test this and get back to you to see if I can replicate this and if the provided code works without any issues.
    Once again thank you for provided details and your patience.

Viewing 4 posts - 1 through 4 (of 4 total)
  • The topic ‘Minify to disk + brotli on nginx’ is closed to new replies.