Gzip compressed file content increases web browsing speed (from: http://www.funnyapple.com/gzip-compress-for-improving-web-speed.html)

Source: Internet
Author: User


We can see the Yahoo homepage's rating in yslow. Today I will introduce how to implement "compress components with gzip. In fact, I personally think this is not what the page reconfigurator is doing, but it also serves as a small branch of front-end development. We also need to know about page restructures, during page reconstruction, be sure not to make your page bloated. A considerable number of programmers may not have taken this issue into consideration. They spend more time pursuing technology, including me at the very beginning.

Now we are paying more and more attention to user experience and how to retain website users. A wide array of dazzling web pages correspond to the increase in the file size! Find a way to slim it down!

Gzip is currently the most popular and effective compression method. Gzip can reduce the response size by about 70%. Currently, about 90% of Internet exchanges transmitted via browsers support the GZIP format. If you are using Apache, the gzip module configuration is related to your version: Apache 1.3 uses mod_zip, while Apache 2.x uses moflate.

The server selects the files that require gzip Compression Based on the file type. Most Web servers compress HTML files. Compression of scripts and style sheets is also worth doing, but many web servers do not have this function. In fact, compressing responses of any text type, including XML and JSON, is worthwhile. Since images and PDF files have been compressed, Gzip compression cannot be performed. If you try to compress these files with gizp, it will not only waste CPU resources, but also increase the file size.

I don't need to talk about it anymore. My server is a win system, so I will first introduce the compression method under the IIS6 server. IIS6 itself supports gzip compression, making iis5 hard. You can go to http://www.port80software.com/products/httpzip? Vid = 3354166 download, but you have to pay the money. :) But now all servers should be upgraded to IIS6! Pray for me!

Pure Tomcat server

If your web application runs under the Tomcat server and directly uses the HTTP service provided by Tomcat, it is too simple. in the xml configuration file, add a compression parameter to HTTP ctor and restart the Tomcat server to take effect immediately. The configuration is as follows:

<Connector port="8080" protocol="HTTP/1.1"                maxThreads="150" connectionTimeout="20000"                redirectPort="8443" compression="on"/>

Tomcat adopts the HTTP/1.1 gzip compression protocol. It determines whether the browser supports the gzip compression protocol based on whether the accept-encoding value in the request sent by the Browser contains gzip, if the browser supports gzip compression, otherwise, no compression will be performed. Tomcat also has another parameter compressablemimetype, which can be used to specify the type of content to be compressed. For example, you can specify the configuration values as text/html, text/plain, only the contenttype is compressed into text/html and text/plain
Page, but you 'd better also calculate the CSS and JavaScript files in the compressed file type, because the compression effect of the two is also very obvious.

Apache server

In Apache 1.3, mod_gzip is commonly used to compress the output content. Currently, mainstream browsers support gzip decompression. In apache2, the module name is mod_deflate, and the corresponding module name is mod_deflate.so. Mod_gzip is not described in this article. The following describes how to enable and configure the mod_deflate module in Apache 2. By default, Apache is not enabled for both Windows and Linux/Unix. This module is not even available in Linux/Unix. You need to compile this module manually.

The following describes how to enable and configure the mod_deflate module in Windows and Linux.

In Windows, the Apache server installed with the installer already has the modules mod_deflate.so and mod_headers.so required by deflate. enable and configure the conf configuration file as follows:

LoadModule deflate_module modules/mod_deflate.soLoadModule headers_module modules/mod_headers.so<Location /># Insert filterSetOutputFilter DEFLATE# Netscape 4.x has some problems...BrowserMatch ^Mozilla/4 gzip-only-text/html# Netscape 4.06-4.08 have some more problemsBrowserMatch ^Mozilla/4\.0[678] no-gzip# MSIE masquerades as Netscape, but it is fine# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48# the above regex won't work. You can use the following# workaround to get the desired effect:BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html# Don't compress imagesSetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary# Make sure proxies don't deliver the wrong contentHeader append Vary User-Agent env=!dont-vary</Location>

If it is a Linux/Unix operating system, if you do not compile the required mod_deflate and mod_headers modules during the compilation and installation process, it will be a little troublesome, first, let's first look at how to compile and install the two modules at the same time during the compilation and installation of Apache. Please add two parameters when executing the configure program:

# ./configure --enable-deflate --enable-headers

After compiling Apache, you can enable and configure the deflate module in httpd. conf. The configuration method is the same as that on Windows.

If your Apache is already running and you do not want to recompile it again, you can compile only the files mod_deflate.c and mod_headers.c required by the deflate module. These two files are located in the {APACHE-Src}/modules/filters/directory (where {APACHE-Src} is the directory where the Apache source file is located ). Use the following command to compile the two source files separately.

# {apache-bin}/apxs -i -a -c {apache-src}/modules/filters/mod_deflate.c# {apache-bin}/apxs –i –a –c {apache-src}/modules/filters/mod_headers.c

{APACHE-bin} is the bin directory under the Apache installation directory. Configure the module directly in httpd. conf.

Many times you may encounter a compilation error when compiling the deflate module separately. The prompt is:

Cannot load/opt/Apache/modules/mod_deflate.so into server:/opt/Apache/modules/mod_deflate.so: Undefined Symbol: deflate

The solution is as follows:

Edit the/usr/local/apache2/bin/APR-config file and change the ldflags value to "-LZ". Then re-compile the mod_deflate module and apxs-ica mod_deflate.c.

To save unnecessary trouble, add the-enable-Deflate-enable-headers parameter during compilation and installation.

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.