简体   繁体   中英

IIS not gzipping on every request

I am currently trying to research whether to implement .NET 4.5 bundling/minification in a project, or stick with our minification via build script that we implemented previously.

While A/B testing, I noticed that (minified) scripts were coming back at different sizes with different requests. The same request would be reported by Chrome Network tab as either 1.55MB or 455KB. When saving the file to disk, Windows was always showing the file at 1,559,907 bytes.

So I took a look at the headers and noticed a difference. The request headers are constant:

GET /WebOpsDev/Scripts/scriptLibsAll.min.js?v=706.19 HTTP/1.1
Host: aburr-dev.pus.local
Connection: keep-alive
Cache-Control: no-cache
Accept: */*
Pragma: no-cache
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Referer: http://aburr-dev.pus.local/WebOpsDev/Account/LogOn
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Cookie: Username=webteam

but the response header is different, specifically the "Content-Encoding" header:

HTTP/1.1 200 OK
Cache-Control: max-age=31536000
Content-Type: application/x-javascript
Last-Modified: Mon, 30 Dec 2013 21:38:42 GMT
Accept-Ranges: bytes
ETag: "99439982a75cf1:0"
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Mon, 30 Dec 2013 21:43:44 GMT
Content-Length: 1559907

HTTP/1.1 200 OK
Cache-Control: max-age=31536000
Content-Type: application/x-javascript
Content-Encoding: gzip
Last-Modified: Mon, 30 Dec 2013 21:38:42 GMT
Accept-Ranges: bytes
ETag: "0dd4082a75cf1:0"
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Mon, 30 Dec 2013 21:42:46 GMT
Content-Length: 463804

My question is, why is gzip only being applied on some requests and not all? What needs to change in order to apply gzip on every request? Also what impact if any will it have on XHR?

Thanks to the responders, but as it turns out IIS doesn't GZIP on every request. In order to get an accurate test I had to switch off Static Content Compression, the opposite of what's described here: http://technet.microsoft.com/en-us/library/cc754668(v=ws.10).aspx

Make sure you have both static and dynamic compression installed in IIS

Ensure that your the file located in C:\\Windows\\System32\\inetsrv\\config\\applicationHost.config

Find the section

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files" minFileSizeForComp="2000">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" dynamicCompressionLevel="9" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/json" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="application/atom+xml" enabled="true" />
<add mimeType="application/xaml+xml" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/json" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="application/atom+xml" enabled="true" />
<add mimeType="application/xaml+xml" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>

Also in your project make sure you use to get the best possible bundling http://www.nuget.org/packages/microsoft.aspnet.web.optimization/ https://www.nuget.org/packages/WebGrease

Use fiddler to check that each request is correctly compressed http://fiddler2.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM