Using Brotli to pre-compress content

Greg M

Active Member
May 26, 2016
43
13
8
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
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,908
2,218
463
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.