无���使IIS7对font-face字体文件进行Gzip压缩

24

我正在使用共享网络主机,并且仅有访问iis7.5的web.config文件。 JavaScript文件和CSS文件已经被gzip压缩,所以它们可以正常工作。但是,我认为这是因为iis7.5启用了静态压缩。然而,我无法对字体文件进行gzip压缩,它们发送时大小相同,响应头也没有content-encoding: gzip。 感谢任何帮助。

这是web.config文件:

<configuration>
<system.webServer>
    <directoryBrowse enabled="false" />
<staticContent>
    <mimeMap fileExtension=".otf" mimeType="font/opentype" />
</staticContent>
<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="font/open-type" enabled="true" />
    <add mimeType="application/javascript" enabled="true" />
    <add mimeType="text/css" enabled="true" />
    <add mimeType="text/html" enabled="true" />
    <add mimeType="*/*" enabled="false" />
        </dynamicTypes>
    <staticTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="font/opentype" enabled="true" />
        <add mimeType="message/*" enabled="true" />
        <add mimeType="application/javascript" enabled="true" />
    <add mimeType="*/*" enabled="false" />
        </staticTypes>
</httpCompression>
    <urlCompression dynamicCompressionBeforeCache="true" doDynamicCompression="true" doStaticCompression="true" />
    <defaultDocument>
        <files>
            <clear />
            <add value="Default.htm" />
            <add value="Default.asp" />
            <add value="index.htm" />
            <add value="index.html" />
            <add value="iisstart.htm" />
            <add value="default.aspx" />
            <add value="index.php" />
        </files>
    </defaultDocument>

</system.webServer>
</configuration>
5个回答

48

默认情况下,IIS在httpCompression模块中不包含这些MIME类型。您需要修改应用程序Host.config文件,在以下位置:C:\ Windows \ System32 \ inetsrv \ config

此文件将影响所有网站,并且必须使用64位Windows中的64位文本编辑器打开。(Notepad2 64位,Notepad,不要使用Notepad ++

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

            <!-- HERE -->
            <add mimeType="image/svg+xml" enabled="true" />
            <add mimeType="application/font-woff" enabled="true" />
            <add mimeType="application/x-font-ttf" enabled="true" />
            <add mimeType="application/octet-stream" enabled="true" />
            <!-- HERE -->

        </staticTypes>
        <dynamicTypes>
            <add mimeType="text/*" enabled="true" />
            <add mimeType="message/*" enabled="true" />
            <add mimeType="application/x-javascript" enabled="true" />
            <add mimeType="*/*" enabled="false" />

            <!-- HERE -->
            <add mimeType="image/svg+xml" enabled="true" />
            <add mimeType="application/font-woff" enabled="true" />
            <add mimeType="application/x-font-ttf" enabled="true" />
            <add mimeType="application/octet-stream" enabled="true" />
            <!-- HERE -->

        </dynamicTypes>
</httpCompression>

这些是我个人的设置,用于压缩SVGWOFFEOTTTF文件。

然后在命令行中键入iisreset以重新加载IIS中的配置,或重启计算机。

更新

WoffWoff2文件已经被压缩,因此您不需要进行此操作。事实上,如果对它们进行gzip压缩,客户端会失去性能。


2
出于好奇,为什么需要64位编辑器?我以前从未听说过这个要求。 - Dan Diplo
5
我已经跟踪并检查了十几次。IIS仍然拒绝使用gzip压缩我的.svg文件。 我成功的唯一方法是将.svg的MIME类型设置为"text/xml",但这样在许多浏览器中无法正确呈现它们。 - Zac
顺便提一下:Notepad++现在也有64位版本 - Uwe Keim
这里是 Notepad 2 的链接 - Uwe Keim
1
@dan-diplo 需要64位编辑器才能打开一些文件路径,除非你适当修改它们:https://serverfault.com/questions/315904/cannot-open-iis-7-applicationhost-config-in-64-bit-windows-with-32-bit-text-edit - George Birbilis
显示剩余3条评论

8
重要的是要注意,修改你的 applicationHost.config(位于 %windir%\system32\inetsrv\config 文件夹)需要注意以下设置:
<section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />

to:

<section name="httpCompression" overrideModeDefault="Allow" />

这将使您能够在 web.config 中的 system.webServer 标签下配置 httpCompression 标签。


1
这不起作用。您实际上必须在 "applicationHost.config" 中添加行 <add mimeType="image/svg+xml" enabled="true" />。即使按照此答案中建议的更改设置,将其设置在“web.config”中也没有效果。(至少在 IIS 8.5、Win8.1 上) - theyetiman

5
问题在于默认情况下,IIS没有将Web字体的MIME类型包括在可以压缩的MIME类型列表中。JavaScript和CSS文件已经包括在内了,这就是为什么你看到它们被压缩了。
你的httpCompression设置可能没有被使用,这些设置默认被锁定,不能在web.config中设置。请查看这个页面:http://support.microsoft.com/kb/969062。在“更多信息”部分中,它说“你只能为Web服务器级别设置MIME类型”。
我在本地服务器上让它正常工作的唯一方法是在applicationHost.config的httpCompression部分中添加MIME类型(这需要管理员权限)。在web.config中设置它们没有任何影响。

4

0

尽管我已经通过IIS远程管理控制台启用了静态和动态压缩,但我仍然遇到了这个问题。

最终,我通过将IIS上.tff文件的MIME类型从application/octet-stream更改为font/ttf来解决了这个问题。


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