IIS:Set 并有条件地提供 gzip 和 brotli 压缩

IIS:Set and serve both gzip and brotli compression conditionally

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

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

为了解决这个问题,我在 IIS 上保留了 Gzip 和 Brotli。但是现在所有浏览器都只下载 Gzip 格式的内容,这很可能是因为请求 header 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>

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

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

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

关于IIS 10.0 Version 1803 or Above,每个压缩方案的优先级由其在元素collection中的顺序决定。

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