0

我想将一些静态内容提供给浏览器,发现与 gzip 相比,Brotli 将最终下载包的大小减少了 43%。

我首先尝试只设置 Brotli,所有现代浏览器都下载压缩文件。但是当尝试使用 IE11(不支持 brotli)时,下载原始内容而不进行任何影响性能的压缩。

为了解决这个问题,我将 Gzip 和 Brotli 都保存在 IIS 上。但是现在所有的浏览器都只下载 Gzip 格式的内容,这很可能是因为请求头的顺序是 gzip 先出现的。

我想让它有条件,以便默认情况下浏览器以 Brotli 格式下载内容,如果浏览器不支持它,则自动切换到 gzip 格式。

知道怎么做吗?

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
        <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
        <scheme name="br" dll="%Windir%\system32\inetsrv\brotli.dll" dynamicCompressionLevel="5" staticCompressionLevel="11" />
        <dynamicTypes>
            <add mimeType="text/*" enabled="true" />
            <add mimeType="message/*" enabled="true" />
            <add mimeType="application/x-javascript" 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="application/atom+xml" enabled="true" />
            <add mimeType="application/xaml+xml" enabled="true" />
            <add mimeType="image/svg+xml" enabled="true" />
            <add mimeType="*/*" enabled="false" />
        </staticTypes>
    </httpCompression>
4

1 回答 1

1

您可以启用 Brotli 和 Gzip 压缩的多种压缩方案,并设置压缩方案的优先级。

IIS 提供了一个默认的压缩方案提供程序 gzip,它默认在 applicationHost.config 中注册为 gzip 方案。但是如果你还想使用 Brotli 压缩,则需要在 applicationHost.config 中添加 iisbrotli.dll 作为 Brotli 压缩方案提供程序。添加 Brotli 压缩方案

关于如何设置 Compression Scheme Prioritization,分为 IIS 10.0 Version 1803 或更高版本和 Before IIS 10.0 Version 1803 两个版本。

关于 IIS 10.0 Version 1803 或更高版本,每个压缩方案的优先级由其在元素集合中的顺序决定。

大约在 IIS 10.0 Version 1803 之前,它会根据 Accept-Encoding 请求标头值中出现的方案顺序来优先压缩方案,但对于浏览器设置 Accept-Encoding 的典型场景,IIS 总是优先 gzip 而不是 br:gzip、deflate、 br 请求中的标头。一种可能的解决方法是安装 URL 重写模块并配置重写规则以修改 Accept-Encoding 标头值。 启用多个压缩方案

于 2020-07-27T09:00:52.687 回答