我的angular2项目为我的网络应用程序构建了预压缩的gzip文件,但我的IIS只提供正常的#34; .js"文件而不是压缩的" .gzip"文件。我的浏览器愿意接受gzip。
IIS允许gzip响应的正确设置是什么?
我已经搜索了google / SO / SU,但只找到了解压缩而不是"预压缩"内容。
答案 0 :(得分:2)
更整洁优雅的解决方案:
注意:文件扩展名.gzip
似乎很奇怪,一般情况下,我们将gziped文件命名为.gz
,因此在此示例中,我们使用.gz
代替.gzip
,如果你坚持.gzip
,只需替换以下配置文件中的所有扩展名。
首先是代码,这就是web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<staticContent>
<remove fileExtension=".js.gz" />
<remove fileExtension=".css.gz" />
<remove fileExtension=".png.gz" />
<remove fileExtension=".jpg.gz" />
<remove fileExtension=".gif.gz" />
<remove fileExtension=".svg.gz" />
<remove fileExtension=".html.gz" />
<remove fileExtension=".json.gz" />
<mimeMap fileExtension=".js.gz" mimeType="application/javascript" />
<mimeMap fileExtension=".css.gz" mimeType="text/css" />
<mimeMap fileExtension=".png.gz" mimeType="image/png" />
<mimeMap fileExtension=".jpg.gz" mimeType="image/jpeg" />
<mimeMap fileExtension=".gif.gz" mimeType="image/gif" />
<mimeMap fileExtension=".svg.gz" mimeType="image/svg+xml" />
<mimeMap fileExtension=".html.gz" mimeType="text/html" />
<mimeMap fileExtension=".json.gz" mimeType="application/json" />
</staticContent>
<rewrite>
<outboundRules rewriteBeforeCache="true">
<rule name="Custom gzip file header">
<match serverVariable="RESPONSE_CONTENT_ENCODING" pattern=".*" />
<conditions>
<add input="{REQUEST_URI}" pattern="\.gz$" />
</conditions>
<action type="Rewrite" value="gzip"/>
</rule>
</outboundRules>
<rules>
<rule name="Rewrite gzip file">
<match url="(.*)"/>
<conditions>
<add input="{HTTP_ACCEPT_ENCODING}" pattern="gzip" />
<add input="{REQUEST_FILENAME}.gz" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:1}.gz" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
以下是它的工作原理:
为了实现成功的gziped数据传输,我们需要:
Accept-Encoding
Content-Encoding
application/gzip
必须同时满足这四个条件。
如果您发送带有Content-Encoding: gzip
的未压缩文件,浏览器将返回错误;
如果您发送的压缩文件没有Content-Encoding
标题或MIME类型不匹配,该页面可能会返回一些Zenith Star的文本。
所以我们正在做的是:
Content-Encoding
Accept-Encoding
,的响应标头的标头
此解决方案适用于我的IIS7,不确定它是否也适用于IIS10。
如果您遇到任何问题,请告诉我:D
答案 1 :(得分:0)
经过很长时间的搜索后,我发现了一个使用URL-Rewrite的解决方法。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
</customHeaders>
</httpProtocol>
<rewrite>
<rules>
<clear />
<rule name="Https redirect" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^domain.com$" />
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
<rule name="LetsEncrypt">
<match url=".well-known/acme-challenge/*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="None" />
</rule>
<rule name="Angular Routes" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
<rule name="br_rewrite" enabled="true" stopProcessing="true">
<match url="(.*).(js$|svg|css)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_ACCEPT_ENCODING}" pattern="br" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:1}.{R:2}.br" logRewrittenUrl="true" />
</rule>
<rule name="gzip_rewrite" enabled="true" stopProcessing="true">
<match url="(.*).(js$|svg|css)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_ACCEPT_ENCODING}" pattern="gzip" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" />
</conditions>
<action type="Rewrite" url="{R:1}.{R:2}.gz" logRewrittenUrl="true" />
</rule>
</rules>
<outboundRules rewriteBeforeCache="true">
<rule name="Remove Server header" enabled="true">
<match serverVariable="RESPONSE_Server" pattern=".+" />
<action type="Rewrite" value="" />
</rule>
<rule name="Rewrite content-encoding header gzip" preCondition="IsGZ" enabled="true" stopProcessing="false">
<match serverVariable="RESPONSE_CONTENT_ENCODING" pattern=".*" />
<action type="Rewrite" value="gzip" />
</rule>
<rule name="Rewrite content-encoding header br" preCondition="IsBR" enabled="true" stopProcessing="false">
<match serverVariable="RESPONSE_CONTENT_ENCODING" pattern=".*" />
<action type="Rewrite" value="br" />
</rule>
<rule name="css content type" preCondition="IsCSS" enabled="true" stopProcessing="false">
<match serverVariable="RESPONSE_CONTENT_TYPE" pattern="(.*)" />
<action type="Rewrite" value="text/css" />
</rule>
<rule name="js content type" preCondition="IsJS" enabled="true" stopProcessing="false">
<match serverVariable="RESPONSE_CONTENT_TYPE" pattern="(.*)" />
<action type="Rewrite" value="application/javascript" />
</rule>
<rule name="svg content type" preCondition="IsSVG" enabled="true" stopProcessing="false">
<match serverVariable="RESPONSE_CONTENT_TYPE" pattern="(.*)" />
<action type="Rewrite" value="image/svg+xml" />
</rule>
<preConditions>
<preCondition name="IsGZ">
<add input="{URL}" pattern="\.gz$" />
</preCondition>
<preCondition name="IsBR">
<add input="{URL}" pattern="\.br$" />
</preCondition>
<preCondition name="IsCSS">
<add input="{URL}" pattern="css" />
</preCondition>
<preCondition name="IsJS">
<add input="{URL}" pattern="js" />
</preCondition>
<preCondition name="IsSVG">
<add input="{URL}" pattern="svg" />
</preCondition>
</preConditions>
</outboundRules>
</rewrite>
<urlCompression doStaticCompression="true" doDynamicCompression="false" />
<httpCompression sendCacheHeaders="false" />
<staticContent>
<mimeMap fileExtension=".br" mimeType="application/brotli" />
<clientCache cacheControlMode="UseMaxAge" />
</staticContent>
</system.webServer>
</configuration>
成功地对BR和GZIP请求预构建角度文件(JS,CSS,SVG)。
我希望这有助于其他人。如果您知道更好的解决方案,请告诉我。