如何在IIS上为SVG文件启用gzip压缩?

10

我创建了一个web.config文件,成功地开启了文本和消息资源的静态压缩。 然而,下面显示的明显解决方案对.svg压缩似乎没有任何影响(通过chrome开发人员工具验证,.svg文件的响应头中未设置gzip内容编码,但对于.html、css等文件已设置)。

以下是我的web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpCompression minFileSizeForComp="1024" MaxDiskSpaceUsage="500">
            <scheme name="gzip"/>
            <staticTypes>
              <add mimeType="text/*" enabled="true"/>
              <add mimeType="message/*" enabled="true"/>
              <add mimeType="application/javascript" enabled="true"/>
              <add mimeType="image/svg+xml" enabled="true"/>
              <add mimeType="application/json" enabled="true" />
              <add mimeType="*/*" enabled="false"/>
            </staticTypes>
        </httpCompression>
        <urlCompression doStaticCompression="true" doDynamicCompression="true"/>
        <staticContent>
          <remove fileExtension=".svg" />
          <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
          <remove fileExtension=".svgz" />
          <mimeMap fileExtension=".svgz" mimeType="image/svg+xml" /> 
        </staticContent>
    </system.webServer>
</configuration>

这个问题的动机是为了按照Google Page Speed Insights建议,提供压缩的SVG字体。我已经在IIS 7.5 / Windows 7和IIS 8 / Windows Server 2012上进行了测试。

有任何想法吗?


你找到答案了吗?我正在使用IIS 8,它会将Content-Encoding: gzip传递给我的SVG文件,但我无法使SVGZ文件正常显示。但是根据Page Speed Insights的说法,即使我可以在Web检查器中看到标头被应用,我的普通SVG也没有被压缩。 - ddilsaver
@ddilsaver 目前还没有解决方案。 - user3072310
解决方案在这里:https://dev59.com/oGYq5IYBdhLWcg3whRDV#23940235 - Dominique Alexandre
1个回答

4

IIS不会压缩太小的文件,并且您可以配置最小大小。在IIS 7.5中, minFileSizeForComp 的默认值为 2700

您的SVG文件太小了吗?我在IIS管理员GUI(而不是web.config)中配置了httpCompression,并且它运行良好。

您可以查看Microsoft的httpCompression配置参考。 示例代码:

<httpCompression
      directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
   <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
   <dynamicTypes>
      <add mimeType="text/*" enabled="true" />
      <add mimeType="message/*" enabled="true" />
      <add mimeType="application/javascript" enabled="true" />
      <add mimeType="*/*" enabled="false" />
   </dynamicTypes>
   <staticTypes>
      <add mimeType="text/*" enabled="true" />
      <add mimeType="message/*" enabled="true" />
      <add mimeType="application/javascript" enabled="true" />
      <add mimeType="*/*" enabled="false" />
   </staticTypes>
</httpCompression>

你可以使用压缩的.svgz文件替代.svg文件以节省CPU。

要为.svgz文件配置gzip内容编码,请参见:如何为特定文件类型添加编码?

<system.webServer>
    <rewrite>
        <outboundRules>
            <rule name="Rewrite SVGZ header" preCondition="IsSVGZ" stopProcessing="true">
                <match serverVariable="RESPONSE_Content_Encoding" pattern=".*" />
                <action type="Rewrite" value="gzip" />
            </rule>
            <preConditions>
                <preCondition name="IsSVGZ">
                    <add input="{PATH_INFO}" pattern="\.svgz$" />
                </preCondition>
            </preConditions>
        </outboundRules>
    </rewrite>
    <staticContent>
        <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
        <mimeMap fileExtension=".svgz" mimeType="image/svg+xml" />
    </staticContent>
</system.webServer>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接