Taken from: desgrange.net
Remember to
sudo a2enmod rewrite
sudo a2enmod headers
sudo apachectl configtest
in your <VirtualHost>
:
<Directory */path/to/my/blog/*>
RewriteEngine on
# Gzip
# If the web browser accept gzip encoding…
RewriteCond %{HTTP:Accept-Encoding} gzip
# …and the web browser is fetching a probably pre-compressed file…
RewriteCond %{REQUEST_URI} .*\.(css|html|js)
# …and a matching pre-compressed file exists…
RewriteCond %{REQUEST_FILENAME}.gz -s
# …then rewrite the request to deliver the gzip file
RewriteRule ^(.+) $1.gz
# For each file format set the correct mime type (otherwise gzip mime type is returned) and prevent Apache for recompressing the files
RewriteRule "\.css\.gz$" "-" [T=text/css,E=no-brotli,E=no-gzip]
RewriteRule "\.html\.gz$" "-" [T=text/html,E=no-brotli,E=no-gzip]
RewriteRule "\.js\.gz$" "-" [T=application/javascript,E=no-brotli,E=no-gzip]
<FilesMatch "\.(css|html|js)\.gz$">
# Serve correct encoding type
Header set Content-Encoding gzip
# Force proxies to cache gzip & non-gzip files separately
Header append Vary Accept-Encoding
</FilesMatch>
</Directory>
Whilst scouring the web for useful pelican plugins, I came across gzip_cache
The idea is that you have a precompressed html.gz / css.gz / js.gz files alongside your html / css / js files.
The above apache2 config should get Apache to serve these already compressed files instead of compressing on the fly.