Last week, I was working on performance improvement of one of my projects. So I read several articles on the Internet and found that we can configure HTTPCompression
on IIS. It compresses the response sent to the browser and the size of the response gets reduced dramatically, i.e., major improvement in performance. So I wanted to share it with you.
I'll discuss it point wise.
1: Why do we need HTTP compression?
Nowadays, we are building Rich Internet Applications, which is increasing the size of Pages heavily. Means the more page size, more time it'll take to load. So IIS provides a feature to compress the responses and most common browsers support the HTTP Compression.
This means you can configure HTTP compression at your web server, and browsers will understand it.
2: How much page size will be reduced?
Normally there are two algorithms supported. One is gzip and the other one is deflate. I used Gzip in my website and found that the Page size was reduced by 60 to 70%.
3: When we don't need compression.
If your page size is very small by default less than 60 to 70k. Then I think you don't need this. Moreover, if your users are having very high speed Internet, then also you can ignore it because obviously the compression/decompression is an overhead if you are not gaining much.
4: How HTTPCompression works.
When a browser sends a request to IIS, it also sends the information about what kinds of encoding it supports. You can see the request header by many tools (One is Firebug that is available as a plugin for Firefox). You will be able to see the following line in the request header.
Accept-Encoding: gzip,deflate
It says that this browser accepts gzip and deflate encoding.
Now when IIS receives the request and finds that the requester can understand the given encoding, based on the configuration, it encodes the response.
Now you can see the Response header. There it is mentioned, in what encoding is done on response as you can see it in the header as:
Content-Encoding: gzip
It means the response was encoded with gzip.
Here I am showing you all an example of Gmail.
Here I am showing the request header sent by the Firefox 3.5.
It says it can accept encoding: gzip,deflate.
Now let’s see the response header:
As you can see, the response is sent in gzipped format.
5: What will happen if the Browser does not understand any encoding?
Actually, when the browser sends a request to the server, it tells the server what all encoding it supports and that is available in Request Header. If it does not support, it may not send the Accept-Encoding tag.
Now when IIS receives the request and if it does not find any encoding mechanism supported by Requester, It does not apply any compression/encoding mechanism and the response is not encoded and sent in normal format.
So you don't need to worry about if any browser does not understand gzip or deflate, then what will happen. IIS takes care, IT only encodes when it is supported by the requester.
6: How to configure HTTPCompression at IIS?
There are two types of Compression that can be configured.
- Static (for static files like some CSS, JavaScript file, etc.)
- Dynamic (means for dynamic generated page/response)
There is no console available to configure HTTPCompression
.
So there are two ways to configure HTTP compression at IIS.
- First: Update the IIS metabase files directly.
- Second: Use some commands to update it.
Here I'll discuss the second one and will discuss the commands that can be used to configure it.
Here you need to do two things.
- First: Configure the IIS for
HTTPCompression
- Second: Configure what all types/extension will be encoded
So for that, you need to run the following commands at your web server:
Configure the IIS6 for HTTP Compression-
First, open command prompt and go to your IIS root folder, normally it would be “c:\inetpub\adminscripts\”, then follow the below steps.
Static Compression
To see whether Compression is enabled or not:
cscript adsutil.vbs get w3svc/filters/compression/parameters/HcDoStaticCompression
Enable/Disable Static Compression:
adsutil.vbs set w3svc/filters/compression/parameters/HcDoStaticCompression true/false
To view what all files will be encoded:
cscript adsutil.vbs get W3SVC/Filters/Compression/gzip/HcFileExtensions (for gzip)
cscript adsutil.vbs get W3SVC/Filters/Compression/deflate/HcFileExtensions (for deflate)
To add more files for Compression:
cscript adsutil.vbs set W3SVC/Filters/Compression/gzip/HcFileExtensions "js" "css"
"png" "bmp" "swf" "doc" "docx" (for gzip)
cscript adsutil.vbs set W3SVC/Filters/Compression/deflate/HcFileExtensions "js" "css"
"png" "bmp" "swf" "doc" "docx" (for deflate)
Dynamic Compression
To see whether Compression is enabled or not:
cscript adsutil.vbs get w3svc/filters/compression/parameters/HcDoDynamicCompression
Enable/Disable Dynamic Compression:
cscript adsutil.vbs set w3svc/filters/compression/parameters/HcDoDynamicCompression true/false
To view what all files will be encoded:
cscript adsutil.vbs get W3SVC/Filters/Compression/gzip/HcScriptFileExtensions (for gzip)
cscript adsutil.vbs get W3SVC/Filters/Compression/deflate/HcScriptFileExtensions (for deflate)
To add more file extension
cscript adsutil.vbs set W3SVC/Filters/Compression/deflate/HcScriptFileExtensions
"asp" "exe" "dll" "aspx" "asmx" (for gzip)
cscript.exe adsutil.vbs set W3Svc/Filters/Compression/GZIP/HcScriptFileExtensions
"asp" "exe" "dll" "aspx" "asmx" (for deflate)
Now reset the IIS. Now your web server is ready to compress the responses based on incoming requests.
I'll suggest to configure it all at IIS6 and see/analyze the performance.
Note: I have explained above the settings for IIS6.
Please share your feedback.
Thanks,
Brij