注意事项
有人认为这是How to serve precompressed gzip/brotli files with .htaccess的复制品。该问题仅寻求提供预压缩文件。这个问题是不同的。请参见下文。
我的目标
我想提供预压缩的brotli文件时,他们存在。如果不存在预压缩的brotli文件,则回退到on-the-flygzip-compression。
当前编码
我正在处理一个已经从.htaccess
文件中启用了动态gzip的网站,如下所示:
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml...
</ifmodule>
修改代码
我已经设置了一个构建脚本,它用brotli压缩了许多静态资产。为了服务它们,我将上面的mod_deflate
块替换为以下代码:
<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>
问题
这在brotli编码的文件按预期存在时提供。然而,我现在面临的问题是,因为剩余的资产在构建时没有进行brotli编码,所以现在没有对它们进行压缩。
我一直无法弄清楚如何使用gzip后备来为brotli提供服务,而不需要为gzip输出进行预压缩。
任何帮助都是赞赏的,谢谢!
1条答案
按热度按时间pobjuy321#
你的问题是你已经用静态的gzip配置替换了动态的gzip配置。
您需要将这两个配置都准备好,还需要更改Brotli代码,将环境设置为
no-gzip
,这样它就不会回退。下面的工作应该是;