Hope this isn't too much of a necro bump. I was interested in trying this out today so I've done the following for my CentOS installation:
- yum install epel-release and enable the repo
- yum install brotli brotli-devel
- Using https://help.directadmin.com/item.php?id=191 add "--with-brotli" to custom/ap2/configure.apache
- ./build apache
- At this point, mod_brotli is installed and technically enabled, but not configured. I found out that even with it configured in /etc/httpd/conf/extra/httpd-includes.conf the browser was sending Accept-encoding gzip, deflate, br so gzip was still being preferred over brotli. So In order to do so, I swapped it for gzip by overriding the extra/http-deflate.conf file
Code:
cd /usr/local/directadmin/custombuild
mkdir -p custom/ap2/conf/extra
cp -Rp configure/ap2/conf/extra/httpd-deflate.conf custom/ap2/conf/extra
Here is my configuration in custom/ap2/conf/extra/extra/httpd-deflate.conf
# Brotli
<IfModule brotli_module>
# Compression
## BrotliCompressionQuality: 0-11 (default: 11)
BrotliCompressionQuality 5
## BrotliCompressionWindow: 10-24 (default: 22)
BrotliCompressionWindow 22
# Logging
# Filter note
BrotliFilterNote Input brotli_in
BrotliFilterNote Output brotli_out
BrotliFilterNote Ratio brotli_ratio
#LogFormat '"%r" %{brotli_out}n/%{brotli_in}n (%{brotli_ratio}n)' brotli
#CustomLog /var/log/httpd/brotli_access.log brotli
# Output filter
AddOutputFilterByType BROTLI_COMPRESS text/html text/plain text/css text/xml
AddOutputFilterByType BROTLI_COMPRESS text/css
AddOutputFilterByType BROTLI_COMPRESS application/x-javascript application/javascript
AddOutputFilterByType BROTLI_COMPRESS application/rss+xml
AddOutputFilterByType BROTLI_COMPRESS application/xml
AddOutputFilterByType BROTLI_COMPRESS application/json
# Global output filer if necessary.
# SetOutputFilter BROTLI
# SetEnvIfNoCase Request_URI \.txt$ no-br
</IfModule>
# End Brotli
Once this is set, run ./build rewrite_confs and the http-deflate.conf will be overwritten with the brotli-specific conf. Most major browsers by now should support this.
(I borrowed some of this from
https://ayesh.me/apache-brotli although at this point, some of the directives mentioned seemed outdated.)
There is probably a better way to accomplish this. Also, I'm still testing the BrotliCompressionQuality value to determine the best compression ratio versus website speed. But for those interested in trying this out, this will probably work as a start.
I'll probably play with this a bit longer here and there to see how well it works, but if anyone has feedback on a more elegant way to get br encoding preferred over gzip that would be interesting.