如何通过Apache2发送压缩(放气)的SVG?

Ale*_*lds 37 .htaccess svg google-chrome apache2

我在网站的.htaccess文件中指定了以下属性:

AddOutputFilterByType DEFLATE image/svg+xml
DeflateCompressionLevel 9
Header append Vary Accept-Encoding
Run Code Online (Sandbox Code Playgroud)

但是,我的SVG资产不是以压缩形式发送的:

$ curl https://example.org/assets/svg/asset.svg --silent -H "Accept-Encoding: gzip,deflate" --write-out "${size_download}\n" --output /dev/null                 
152655                                                                                                                                                                                                                                  
$ curl https://example.org/assets/svg/asset.svg --silent --write-out "%{size_download}\n" --output /dev/null
152655
Run Code Online (Sandbox Code Playgroud)

我验证了此资产(asset.svg)是image/svg+xml使用Chrome 以MIME类型发送的,但是使用Web Developer工具时,此特定文件在发送到客户端时不会被压缩.

将其他MIME类型添加到.htaccess文件是成功的(例如,添加text/html压缩HTML资源).

这似乎特定于如何处理SVG数据.我还可以尝试或排除故障以使SVG压缩工作?

Ree*_*eno 72

如果Apache不知道文件的mime类型(这里是image/svg + xml),你需要具体告诉它(在大多数Apaches中不需要):

AddType image/svg+xml svg svgz
Run Code Online (Sandbox Code Playgroud)

现在当Apache知道文件类型时,只需添加它以对其进行deflate:

AddOutputFilterByType DEFLATE image/svg+xml
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅https://httpd.apache.org/docs/2.4/mod/mod_deflate.html

  • 其实,我错了。我在父 conf 文件中有 `AllowOverride None`,以及使用其他 MIME 类型压缩文件的输出过滤器。一旦我将该指令更改为“AllowOverride All”,一切就正常了。精彩,加油! (2认同)