Website performance optimization-gzip Compression

Source: Internet
Author: User
Tags website performance metabase

Gzip compression actually compresses webpage content to reduce htmlCodeThe cost of network transmission to improve web performance.

The request process is explained.:

1: client request. Http_header will send the corresponding encoding rules according to the corresponding browser,

This indicates that the "I" client browser supports gzip and deflate compression formats.

2: when the server receives this "Accept-encodeing", the server can use the corresponding gizp and defalte to compress the web content and respond to it.

3: the client browser receives the Compressed Content, decompress the content according to "content-encoding: gzip", and present it to the user.

 

How to enable gzip compression in iis6.0

    • open the Internet Information Service (IIS) manager, right-click "website"-> "properties", and select "service ". In the "HTTP compression" box, select "compressing Program Files" and "compressing static files ", set "temporary directory" and "maximum temporary directory limit" as needed.
    • in the Internet Information Service (IIS) manager, right-click "Web Service extension"-> "Add a new Web Service extension... ", enter the extension" HTTP compression "in the" New web service extension "box, and add" required file "to c: \ windows \ system32 \ inetsrv \ gzip. DLL, the Windows System directory may vary depending on your installation, select "set extension status to allow";
    • Open c: \ windows \ system32 \ inetsrv \ metabase in a text editor. in XML format (backup is recommended), locate location = "/lm/w3svc/filters/compression/gzip". To Compress dynamic files, set hcdodynamiccompression to "true ", add the suffix of the dynamic file you want to compress in hcscriptfileextensions, such as Aspx. If you want to compress static files, set hcdostaticcompression and hcdoondemancompression to "true ", add the extension names of the static files to be compressed in hcfileextensions, such as XML and CSS. hcdynamiccompressionlevel and hcondemandcomplevel indicate the compression ratio. The smaller the number, the lower the compression ratio.
    • Save the metabase. xml file after editing. If the file cannot be saved, IIS may be using the file. Open "start"-> "Administrative Tools"-> "service", stop "IIS Admin Service", and save the settings.
    • Finally, restart IIS. You can go to the HTTP compression test site to verify the results.

Here we should mention that iis6.0 compression will optimize static files such as HTML and CSS. Iis6.0 puts these compressed files in a cache directory. If they are not modified, iis6.0 will directly return the files in the cache directory. Instead of compressing these files each time.

Enable gzip compression (. NET 2.0) through httpmodules)

NET 2.0 has two classes: gzipstream deflatestream. Paste the module code as follows (non-original)

Public sealed class compressionmodule: ihttpmodule
{

Void ihttpmodule. Dispose ()
{
// Nothing to dispose;
}

Void ihttpmodule. INIT (httpapplication context)
{
If (blogsettings. instance. enablehttpcompression)
{
Context. prerequesthandlerexecute + = new eventhandler (context_postreleaserequeststate );
}
}

Private const string gzip = "gzip ";
Private const string deflate = "deflate ";

Void context_postreleaserequeststate (Object sender, eventargs E)
{
Httpapplication APP = (httpapplication) sender;
If (App. Context. currenthandler is system. Web. UI. Page & App. request ["http_x_microsoftajax"] = NULL)
{
If (isencodingaccepted (deflate ))
{
App. response. Filter = new deflatestream (App. response. filter, compressionmode. Compress );
Setencoding (deflate );
}
Else if (isencodingaccepted (gzip ))
{
App. response. Filter = new gzipstream (App. response. filter, compressionmode. Compress );
Setencoding (gzip );
}
}
Else if (App. Context. Request. Path. Contains ("webresource. axd "))
{
App. Context. response. cache. setexpires (datetime. Now. adddays (30); // It Is A. Net file. The cache time is added here.
}
}

Private Static bool isencodingaccepted (string encoding)
{
Httpcontext context = httpcontext. Current;
Return context. Request. headers ["Accept-encoding"]! = NULL & context. Request. headers ["Accept-encoding"]. Contains (encoding );
}

Private Static void setencoding (string encoding)
{
Httpcontext. Current. response. appendheader ("content-encoding", encoding );
}

}

What types of files should be compressed by gzip?:

1: CSS, JavaScript, HTML static files

2: ASP and aspx dynamic files

Do not use gzip to compress images, but increase the size.ArticleMentioned)

 

How to check whether the webpage has been compressed by gzip:

1: fiddler software (the software in my use), View content-Encoding

2: http://www.port80software.com/tools/compresscheck.asp website

3: Write a post by yourself and add "Accept-encodeing: gzip". You can see the returned content.

 

Improvement:

Currently, Gzip compression has been enabled for the ASPX page of our website, but CSS, JavaScript, and HTML are not yet used. In addition, it is best to use iis6.0 configuration compression (iis6.0 has a compression cache ).

Ask:

1: What is the difference between gzip and deflate?

2: From the htmpmodule code, defalte should be used first. Is deflate preferred if both gzip and deflate exist. However, we found that the compression ratio of defalte is big in gzip?

Reference:

Dudu http://www.cnblogs.com/dudu/archive/2004/12/12/76085.html

Http://www.yaosansi.com/post/957.html

-- =Happy reading= --

Google Tag: Web performance optimization, Gzip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.