首页 文章

IIS应用程序缺少Content-Encoding - 响应标头中的gzip

提问于
浏览
9

在Firebug中,请求标头具有以下条目:
Accept-Encoding: gzip, deflate

但是没有:
Content-Encoding: gzip
在响应标头中 .

无论我尝试过什么,在SO和其他网站上的一些答案后,似乎没有任何作用!静态文件或动态文件都没有被压缩,或者至少如果它们没有内容编码 - gzip值会在响应头中返回 .

以下是我的web.config设置示例:

<urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true" />
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files" minFileSizeForComp="150" staticCompressionIgnoreHitFrequency="true">
  <remove name="gzip" />
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="8" dynamicCompressionLevel="8" />
</httpCompression>

我忽略了命中频率
staticCompressionIgnoreHitFrequency="true

我已经确认IIS实际上压缩了我可以看到的文件:
C:\inetpub\temp\IIS Temporary Compressed Files

如此处指定:set up gzip in IIS 8 windows 8
我已确保在Windows功能> Internet信息服务> WWW服务>性能功能中启用静态和动态压缩

我_2496601的方法:
IIS 7.5 Compression creates compressed file but returns the non-compressed one


Edit 1:
IIS版本是10,但我也在IIS 8.5上尝试过这个版本


Edit 2:
我现在也尝试了在这个链接上找到的各种配置文件:https://github.com/h5bp/server-configs-iis/,它提供了一些看起来像'best practice' web.config文件的内容 .
Not solved


Edit 3:
基于@Nkosi 's input I created a completely new Asp.net MVC application and configured it using all these options I' ve尝试过 . 这是我从Fiddler得到的原始 Headers :

HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: text/javascript; charset=UTF-8
Expires: Wed, 20 Jul 2016 18:22:47 GMT
Last-Modified: Wed, 20 Jul 2016 18:22:47 GMT
Server: Microsoft-HTTPAPI/2.0
Date: Wed, 20 Jul 2016 18:22:47 GMT

如您所见,没有Content-Encoding:Gzip
Not solved


Edit 4:
我尝试过这种方法将代码添加到Global.asax部分的BeginRequest事件中:https://stackoverflow.com/a/27185575/392591
Not solved


Edit 5:
所以我只是尝试在SO上根据这个答案启用跟踪:https://stackoverflow.com/a/33182525/392591
没有失败,但我确实注意到跟踪文件的底部有's a section called GENERAL_RESPONSE_HEADERS and here' s它提供的内容:

Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Server: Microsoft-IIS/10.0
X-AspNetMvc-Version: 5.2
X-AspNet-Version: 4.0.30319
X-Powered-By: My Little Pony
X-UA-Compatible: IE=Edge,chrome=1

这适用于每个静态类型文件 .
但是我刚在跟踪文件中找到以下内容:

8. STATIC_COMPRESSION_START  08:04:03.552 
9. STATIC_COMPRESSION_NOT_SUCCESS Reason="NOT_FREQUENTLY_HIT" 08:04:03.552 
10. STATIC_COMPRESSION_END  08:04:03.552

压缩不成功的原因并不常见......很奇怪,因为我确实将Ignore Hit Frequency选项设置为true!

所以我刚进入IIS管理器并在服务器上将Ignore Hit Frequency设置为true(即applicationHost.config),它将跟踪文件输出更改为以下内容:

8. STATIC_COMPRESSION_START  08:19:17.489 
9. STATIC_COMPRESSION_SUCCESS  08:19:17.489 
10. STATIC_COMPRESSION_END  08:19:17.489

我回去并在applicationHost.config中将其关闭,然后又回到静态压缩不成功,所以这肯定会有所不同 . 但是,当我查看FireBug时,它仍然传递未压缩文件而没有GZIP内容编码响应头 .

我在失败的请求跟踪中注意到的另一个有趣的位是最后两个entires GENERAL_FLUSH_RESPONSE_END和GENERAL_REQUEST_END两个都显示我的Bootstrap.css文件发送了17903个字节,大约18kb,与我在IIS临时压缩中看到的文件的压缩版本相匹配文件夹 . 因此文件在物理上被压缩,根据失败的请求跟踪,它发送正确的内容......但是浏览器会选择完整的117kb文件?
Not solved


3 回答

  • 0

    我正在使用IIS10和我的web.config

    <system.webServer>
        <urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="false" />
        <!-- other config removed for brevity -->
    </system.webServer>
    

    当我测试从浏览器(Firefox,IE11,Edge,谷歌Chrome)到简单的MVC应用程序的请求时 .

    请求都有 Accept-Encoding: gzip, deflate ,响应返回 Content-Encoding:gzip .

    我甚至用Fiddler测试过它 . 手动编写请求

    GET http://localhost/MyWebApplication HTTP/1.1
    User-Agent: Fiddler
    Host: localhost
    Accept-Encoding: gzip, deflate
    

    并得到相同的结果

    HTTP/1.1 200 OK
    Cache-Control: private
    Content-Type: text/html; charset=utf-8
    Content-Encoding: gzip
    Vary: Accept-Encoding
    Server: Microsoft-IIS/10.0
    X-AspNetMvc-Version: 5.2
    X-AspNet-Version: 4.0.30319
    X-Powered-By: ASP.NET
    Date: Mon, 18 Jul 2016 15:26:06 GMT
    Content-Length: 3826
    
    ...
    

    正在压缩Css,Js和所有其他基于文本的文件 .

    您可能需要重新检查配置,以确保在IIS和web.config中正确配置了压缩 .

    更新:

    我注意到图像没有被压缩

    请求

    GET http://localhost/MyWebApplication/Images/Logo_small.png HTTP/1.1
    User-Agent: Fiddler
    Host: localhost
    Accept-Encoding: gzip, deflate
    

    响应

    HTTP/1.1 200 OK
    Cache-Control: max-age=604800
    Content-Type: image/png
    Last-Modified: Fri, 27 Nov 2015 03:15:22 GMT
    Accept-Ranges: bytes
    ETag: "c9d1fdd9c128d11:0"
    Server: Microsoft-IIS/10.0
    X-Powered-By: ASP.NET
    Date: Mon, 18 Jul 2016 15:33:02 GMT
    Content-Length: 2970
    
    ...
    

    并且在一些google-fu发现图像通常已经被压缩后因此没有应用gzip .

    FULL system.webServer from web.config

    <system.webServer>
        <urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="false" />
       <validation validateIntegratedModeConfiguration="false" />
        <httpErrors errorMode="Custom" existingResponse="Replace">
          <clear />
          <error statusCode="404" responseMode="ExecuteURL" path="/NotFound" />
        </httpErrors>
        <handlers>
          <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
          <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
          <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
          <remove name="OPTIONSVerbHandler" />
          <remove name="TRACEVerbHandler" />
          <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
          <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
          <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>
        <staticContent>
          <remove fileExtension=".woff" />
          <remove fileExtension=".woff2" />
          <mimeMap fileExtension=".woff" mimeType="application/font-woff" />
          <mimeMap fileExtension=".woff2" mimeType="application/font-woff2" />
          <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
        </staticContent>
      </system.webServer>
    
  • 1

    我对预配置的IIS有类似的问题 .

    经过多次尝试,我们发现问题是GZIP安装中的一个错误,只需重新安装控制面板中的功能即可解决问题 .

  • 0

    我有类似IIS和gzip配置的情况

    在Firebug中,请求标头具有以下条目:Accept-Encoding:gzip,deflate但是没有:Content-Encoding:gzip在响应标头中 .

    在我的情况下问题是 antivirus protection . 实际上已经应用了gzipping但 antivirus 已启用设置 protect http connections (取决于具体程序),解压缩响应检查它以及动态重写响应标头之后 .

    NOTE :当某些代理/防病毒软件更改了您的响应标头时的一个关键属性,它是在消失时 Content-LengthTransfer-Encoding 添加了值 chunked .

相关问题