10

我创建了一个 web.config 文件,它成功地打开了文本和消息资源的静态压缩。但是,下面显示的明显解决方案似乎对 .svg 压缩没有任何影响(验证了 gzip 内容编码未在 .svg 文件的响应标头中设置,而是通过 chrome 开发人员工具为 .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 上测试这个 web.config。

有任何想法吗?

4

1 回答 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>
于 2015-04-13T01:36:23.847 回答