Brotli跟GZIP一樣是一種網頁的壓縮技術,可以用來壓縮javascript , css ,html 等,減少流量使用並增加率加載速度。
Brotli比GZIP壓縮效率更好,壓縮速度也比較快,十分建議使用。
# 安裝需要的套件,當套件更新,可能需要重新編譯,否則可能無法啟動apache yum -y install libtool automake httpd-devel # git clone 最後一次 commit,且連同 子模組 一起 clone 回來 git clone --depth=1 --recursive https://github.com/kjdev/apache-mod-brotli.git cd apache-mod-brotli ./autogen.sh ./configure make # 編譯完成 # 安裝 install -p -m 755 -D .libs/mod_brotli.so /etc/httpd/modules/mod_brotli.so
編譯好後再來需要設定,需放在<ifModule mod_deflate.c>前面,如此才可優先使用Brotli。
以下是官方建議的設定
# Load module LoadModule brotli_module modules/mod_brotli.so <IfModule brotli_module> # Output filter AddOutputFilterByType BROTLI text/html text/plain text/css text/xml # SetOutputFilter BROTLI # SetEnvIfNoCase Request_URI \.txt$ no-br # Compression ## BrotliCompressionLevel: 0-11 (default: 11) BrotliCompressionLevel 10 ## BrotliWindowSize: 10-24 (default: 22) BrotliWindowSize 22 # Specifies how to change the ETag header when the response is compressed ## BrotliAlterEtag: AddSuffix, NoChange, Remove (default: AddSuffix) BrotliAlterEtag AddSuffix # 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 logs/access_log brotli </IfModule>
可以自行調整
<ifmodule brotli_module> # Output filter AddOutputFilterByType BROTLI text/html text/plain text/css text/xml # Compress HTML, CSS, JavaScript, Text, XML and fonts (此段我自己加的) AddOutputFilterByType BROTLI application/javascript AddOutputFilterByType BROTLI application/rss+xml AddOutputFilterByType BROTLI application/vnd.ms-fontobject AddOutputFilterByType BROTLI application/x-font AddOutputFilterByType BROTLI application/x-font-opentype AddOutputFilterByType BROTLI application/x-font-otf AddOutputFilterByType BROTLI application/x-font-truetype AddOutputFilterByType BROTLI application/x-font-ttf AddOutputFilterByType BROTLI application/x-javascript AddOutputFilterByType BROTLI application/xhtml+xml AddOutputFilterByType BROTLI application/xml AddOutputFilterByType BROTLI font/opentype AddOutputFilterByType BROTLI font/otf AddOutputFilterByType BROTLI font/ttf AddOutputFilterByType BROTLI image/svg+xml AddOutputFilterByType BROTLI image/x-icon AddOutputFilterByType BROTLI text/css AddOutputFilterByType BROTLI text/html AddOutputFilterByType BROTLI text/javascript AddOutputFilterByType BROTLI text/plain AddOutputFilterByType BROTLI text/xml # SetOutputFilter BROTLI # SetEnvIfNoCase Request_URI \.txt$ no-br # Compression ## BrotliCompressionLevel: 0-11 (default: 11) BrotliCompressionLevel 10 ## BrotliWindowSize: 10-24 (default: 22) BrotliWindowSize 22 # Specifies how to change the ETag header when the response is compressed ## BrotliAlterEtag: AddSuffix, NoChange, Remove (default: AddSuffix) BrotliAlterEtag AddSuffix # 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 logs/access_log brotli </IfModule>