Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Using Brotli to pre-compress content

Discussion in 'General Discussion' started by Greg M, Jul 19, 2018.

Tags:
  1. Greg M

    Greg M Active Member

    Joined:
    May 26, 2016
    Messages:
    27
    Likes Received:
    5
    Trophy Points:
    3
    Location:
    Glasgow, Scotland
    cPanel Access Level:
    Root Administrator
    Hello,

    Been using brotli for a while now, its very good in general. However I have failed to get the pre-compressed part working. It would appear that brotli is compressing on the fly each time rather than saving the files and serving those.

    I have set up the config so that it is meant to serve pre-compressed it just doesn't seem to work. Would love some help.

    /etc/apache2/conf.d/brotli.conf

    Code:
    <IfModule brotli_module>
      # Compress only a few types
      # https://httpd.apache.org/docs/trunk/mod/mod_brotli.html
      AddOutputFilterByType BROTLI_COMPRESS text/plain text/css text/html text/svg application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript application/x-font-ttf application/vnd.ms-fontobject image/x-icon
    
      SetOutputFilter BROTLI_COMPRESS
      SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|swf|woff|woff2)$ no-brotli
    
    
     # logging
    
    BrotliFilterNote Input instream
    BrotliFilterNote Output outstream
    BrotliFilterNote Ratio ratio
    
    LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' brotli
    CustomLog "logs/brotli_log" brotli
    </IfModule>
    


    the pre-compression stuff has been added to -> post_virtualhost_global.conf

    Code:
    <IfModule mod_headers.c>
        # Serve brotli compressed CSS and JS files if they exist
        # and the client accepts brotli.
        RewriteCond "%{HTTP:Accept-encoding}" "br"
        RewriteCond "%{REQUEST_FILENAME}\.br" "-s"
        RewriteRule "^(.*)\.(js|css)"              "$1\.$2\.br" [QSA]
    
        # Serve correct content types, and prevent double compression.
        RewriteRule "\.css\.br$" "-" [T=text/css,E=no-brotli:1]
        RewriteRule "\.js\.br$"  "-" [T=text/javascript,E=no-brotli:1]
    
    
        <FilesMatch "(\.js\.br|\.css\.br)$">
          # Serve correct encoding type.
          Header append Content-Encoding br
    
          # Force proxies to cache brotli &
          # non-brotli css/js files separately.
          Header append Vary Accept-Encoding
        </FilesMatch>
    </IfModule>


    According to official docs I should have pre-compressed files being served. But if I disable brotli or remove the conf file (while leaving include in lace) comrpessed files are not served. Also when trying to search for the files I do not find anything with .br for any sites

    Appreciate any help given
     
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    44,344
    Likes Received:
    1,852
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello Greg,

    Can you revert all of the previous modifications you have performed to serve pre-compressed content with the Apache Brotli module? After you do that, browse to WHM >> Apache Configuration >> Include Editor and add the following to the Pre VirtualHost Include section:

    Code:
    <IfModule mod_headers.c>
        # Serve brotli compressed CSS and JS files if they exist
        # and the client accepts brotli.
        RewriteCond "%{HTTP:Accept-encoding}" "br"
        RewriteCond "%{REQUEST_FILENAME}\.br" "-s"
        RewriteRule "^(.*)\.(js|css)"              "$1\.$2\.br" [QSA]
    
        # Serve correct content types, and prevent double compression.
        RewriteRule "\.css\.br$" "-" [T=text/css,E=no-brotli:1]
        RewriteRule "\.js\.br$"  "-" [T=text/javascript,E=no-brotli:1]
    
    
        <FilesMatch "(\.js\.br|\.css\.br)$">
          # Serve correct encoding type.
          Header append Content-Encoding br
    
          # Force proxies to cache brotli &
          # non-brotli css/js files separately.
          Header append Vary Accept-Encoding
        </FilesMatch>
    </IfModule>
    This looks to match the entry you used, but it's instead added as part of the Pre VirtualHost Include section. If the issue persists, can you let us know the exact steps you are using to verify if the content is pre-compressed?

    Thank you.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice