在IIS7/8/8.5中指定多个(gzip + brotli)httpCompression方案并优先化brotli [英] Specify multiple (gzip + brotli) httpCompression schemes in IIS7/8/8.5 and prioritise brotli
问题描述
我正在尝试使用" Brotli 压缩方案. ="https://www.iispeed.com/pagespeed/products/iisbrotli" rel ="noreferrer">用于Microsoft IIS的Brotli压缩模块"通过 iisspeed.com .
I'm trying to get the new Brotli compression scheme working in IIS using "Brotli compression module for Microsoft IIS" by iisspeed.com.
如果我将applicationHost.config
中的<httpCompression>
配置部分更改为仅具有Brotli模块,则Brotli压缩模块本身可以正常工作.
The Brotli compression module itself works fine if I change the <httpCompression>
config section in applicationHost.config
to only have the Brotli module.
iisspeed.com上的文档说要这样做:
The documentation on iisspeed.com says to do this:
<httpCompression directory="path\to\temp\folder" minFileSizeForComp="50">
<scheme name="br" dll="path\to\iisbrotli.dll" />
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
...
</httpCompression>
但是我发现这行不通.
浏览器(在此示例中为Chrome)发送以下accept-encoding
标头:
The browser (Chrome in this example) sends the following accept-encoding
header:
accept-encoding: gzip, deflate, sdch, br
这意味着浏览器可以接受Brotli编码br
和gzip
. 我希望IIS比gzip
更喜欢br
,但是似乎没有一种方法可以对配置中的每个<scheme>
元素进行优先级排序.我尝试更改.config文件中的顺序,但没有效果.
This means the browser can accept Brotli encoding br
as well as gzip
. I want IIS to prefer br
over gzip
but there doesn't appear to be a way to prioritise each <scheme>
element in the config. I've tried changing the order in the .config file but it has no effect.
即使支持br
,IIS仍始终使用gzip
,因此它是首选文件,因为它的文件大小较小.
IIS always uses gzip
even though br
is supported and would be preferred because it's a smaller file size.
我曾搜寻过Google,以发现IIS 6中曾经为每个压缩方案设置了优先级设置,但在IIS7 +中似乎已将其删除.
I have scoured Google to find that there used to be a priority setting for each compression scheme in IIS 6 but it seems to have been removed in IIS7+.
它称为HcPriority
,并进入IIS6元数据库XML文件.
It's called HcPriority
and went into the IIS6 metabase XML file.
请参阅以下链接:
https://msdn.microsoft.com /en-us/library/ms525366(v=vs.90).aspx
https://blogs.iis.net/ksingla/changes- to-compression-in-iis7
https://forums.iis.net/t/1150520.aspx
推荐答案
您所引用的Brotli模块似乎需要付费许可证,因此我没有尝试过,但是我自己的用于IIS的开源Brotli插件.
It appears the Brotli module you referenced requires a paid license, so I haven't tried it, but I encountered a similar issue with my own open source Brotli plugin for IIS.
正如您所指出的那样,当前的浏览器在Accept-Encoding
标头中的gzip
和deflate
之后宣传Brotli支持.
As you pointed out, current browsers advertise Brotli support after gzip
and deflate
in the Accept-Encoding
header.
HTTP RFC 没有提供有关如何选择的具体指导许多具有相同优先级的Accept-Encoding
值,因此可以将br
内容返回给那些客户端是可以接受的.但是,IIS似乎总是选择与其配置的压缩方案之一匹配的第一个(从左到右).
The HTTP RFC gives no specific guidance on how to choose from many Accept-Encoding
values with the same priority, so it would be acceptable to return br
content to those clients. However, it appears IIS always chooses the first one (left to right) that matches one of its configured compression schemes.
如果希望两个方案都保持启用状态,则可以在请求进入IIS管道时修改请求的Accept-Encoding
标头值. IIS URL重写模块可以通过简单的规则来做到这一点.
If you wish to leave both schemes enabled, you can modify the Accept-Encoding
header value on requests as they enter your IIS pipeline. The IIS URL Rewrite Module can do this with a simple rule.
Accept-Encoding
标头由IIS管道中的HTTP_ACCEPT_ENCODING
服务器变量表示,可以在到达压缩模块之前对其进行修改.这是一个示例配置:
The Accept-Encoding
header is represented by the HTTP_ACCEPT_ENCODING
Server Variable in the IIS pipeline, and you can modify it before it reaches the Compression Module(s). Here is a sample configuration:
<rewrite>
<allowedServerVariables>
<add name="HTTP_ACCEPT_ENCODING" />
</allowedServerVariables>
<rules>
<rule name="Prioritize Brotli">
<match url=".*" />
<conditions>
<add input="{HTTP_ACCEPT_ENCODING}" pattern="\bbr(?!;q=0)\b" />
</conditions>
<serverVariables>
<set name="HTTP_ACCEPT_ENCODING" value="br" />
</serverVariables>
</rule>
</rules>
</rewrite>
上面的规则在Accept-Encoding
标头中查找字符串br
(由单词边界包围-而不是紧跟在;q=0
之后),并将其重新编写为纯文本br
,从而得到IIS只有一种选择.
The rule above looks for the string br
(surrounded by word boundaries -- and not immediately followed by ;q=0
) in the Accept-Encoding
header and re-writes it to be just plain br
, giving IIS only one choice.
请注意,默认的URL重写配置不允许修改HTTP_ACCEPT_ENCODING
变量. allowedServerVariables
元素将覆盖该限制,并且必须在applicationHost.config
中进行配置.可以在配置层次结构的任何级别上定义重写规则,尽管将其设置为全局可能很有意义.
Note that the default URL Rewrite configuration does not allow modification of the HTTP_ACCEPT_ENCODING
variable. The allowedServerVariables
element overrides that restriction and must be configured in applicationHost.config
. The rewrite rule can then be defined at any level in the config hierarchy, although it probably makes sense to make it global.
这篇关于在IIS7/8/8.5中指定多个(gzip + brotli)httpCompression方案并优先化brotli的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!