1. Introduction to gzip
gzip is the abbreviation of GNU zip. It is a GNU free software file compression program and is often used to represent the file format gzip. The authors of the software are Jean-loup Gailly and Mark Adler. It was first publicly released on October 31, 1992. The version number is 0.1. The current stable version is 1.2.4.
Gzip is mainly used for file compression in Unix systems. We often use files with the suffix .gz in Linux, and they are in GZIP format. Nowadays, it has become a very common data compression format, or file format, used on the Internet. When applying Gzip compression to a plain text file, the effect is very obvious. After GZIP compression, the page size can become 40% or less of the original size, depending on the content of the file.
GZIP encoding over HTTP protocol is a technology used to improve the performance of WEB applications. In web development, you can use gzip to compress pages to reduce website traffic. However, gzip does not occupy a lot of CPU. The increase is only a few percentage points, but it can compress pages by more than 30%, which is very cost-effective.
Using the Gzip module in Apache, we can use the Gzip compression algorithm to compress the web page content published by the Apache server and then transmit it to the client browser. This compression actually reduces the number of bytes transmitted over the network (saving network I/O for transmission). The most obvious benefit is that it can speed up the loading of web pages.
The benefits of faster web page loading are self-evident. In addition to saving traffic and improving the user's browsing experience, another potential benefit is that Gzip has a better relationship with search engine crawlers. For example, Google can crawl web pages faster than ordinary manual crawling by reading gzip files directly. In Google Webmaster Tools you can see that sitemap.xml.gz is submitted directly as a Sitemap.
And these benefits are not limited to static content, PHP dynamic pages and other dynamically generated content can be compressed by using the Apache compression module, coupled with other performance adjustment mechanisms and corresponding server-side caching rules, this can be greatly improved Website performance. Therefore, for PHP programs deployed on Linux servers, we recommend that you enable Gzip Web compression if the server supports it.
2. The process of Web server processing HTTP compression is as follows:
1. After receiving the HTTP request from the browser, the web server checks whether the browser supports HTTP compression (Accept-Encoding information);
2. If the browser supports HTTP compression, the web server checks the suffix of the requested file;
3. If the requested file is a static file such as HTML, CSS, etc., the web server will check whether the latest compressed file of the requested file already exists in the compression buffer directory;
4. If the compressed file of the requested file does not exist, the web server returns the uncompressed requested file to the browser and stores the compressed file of the requested file in the compression buffer directory;
5. If the latest compressed file of the requested file already exists, the compressed file of the requested file will be returned directly;
6. If the requested file is a dynamic file, the web server dynamically compresses the content and returns it to the browser. The compressed content is not stored in the compression cache directory.
3. Enable apache’s gzip function
There are two modules on Apache that use the Gzip compression algorithm for compression: mod_gzip and mod_deflate. To use Gzip web compression, first make sure your server has support for one of these two components.
Although using Gzip also requires the support of the client browser, don't worry, most browsers currently support Gzip, such as IE, Mozilla Firefox, Opera, Chrome, etc.
By looking at the HTTP header, we can quickly determine whether the client browser used supports gzip compression. If the following information appears in the sent HTTP header, it means that your browser supports the corresponding gzip compression:
The code is as follows:
View with firebug:
Accept-Encoding: gzip,deflate supports both mod_gzip and mod_deflate
If the server enables support for the Gzip component, then we can customize it in http.conf or .htaccess. The following is a simple example of .htaccess configuration:
mod_gzip configuration:
The code is as follows:
# mod_gzip: pl)$Configuration example of mod_deflate:
Open
will
#LoadModule deflate_module modules/mod_deflate.so
#LoadModule headers_module modules/mod_headers.so
Remove the # sign at the beginning
The code is as follows:
# mod_deflate:
ri
The following file MIME types can be added according to your own situation. As for PDF
, pictures, music documents and the like are already highly compressed formats. Repeated compression has little effect. On the contrary, it may reduce performance by increasing CPU processing time and browser rendering problems. So there is no need
To be compressed again via Gzip. After passing the above settings, check the returned HTTP header. If the following information appears, it means that the returned data has been compressed. That is, the Gzip compression configured in the website program has taken effect.
firebug view:
Content-Encoding: gzip
Note:
1) Regardless of using mod_gzip or mod_deflate, the information returned here is the same. Because they all implement gzip compression.
2) CompressionLevel 9 refers to the level of compression (setting the compression ratio). The value ranges from 1 to 9, with 9 being the highest level. It is understood that this can reduce the size of the transfer by up to 80% (depending on the file content), and can save at least half. CompressionLevel can be set to a value of 6 by default to maintain a balance between processor performance and web page compression quality. It is not recommended to set it too high. If it is set to a high level, although it will have a high compression rate, it will take up more CPU resources.
3) There is no need to compress image formats such as jpg, music files such as mp3, and compressed files such as zip that have already been compressed.
4. What are the main differences between mod_gzip and mod_deflate? Which one is better to use?
The first difference is the version of the Apache web server in which they are installed:
The Apache 1.x
series does not have built-in web page compression technology, so an additional third-party mod_gzip module is used to perform compression. WhenApache 2.x was officially developed, web page compression was taken into consideration and the mod_deflate module was built-in to replace mod_gzip. Although both use the Gzip compression algorithm, their operating principles are similar. The second difference is the compression quality:
mod_deflate has a slightly faster compression speed and mod_gzip has a slightly higher compression ratio. Generally, by default, mod_gzip will achieve 4% to 6% more compression than mod_deflate. So, why use mod_deflate?
The third difference is the occupation of server resources:
Generally speaking, mod_gzip consumes a higher amount of server CPU. mod_deflate It is a compression module specially used to ensure the performance of the server, mod_deflate Less resources are required to compress files. This means that on a high-traffic server, using mod_deflate may load faster than mod_gzip.
Don’t quite understand? In short, if your website has less than 1,000 unique visitors per day and you want to speed up the loading of web pages, use mod_gzip. Although it will consume some additional server resources, But it's worth it. If your website has more than 1,000 unique visitors per day and is using a shared virtual host with limited allocated system resources, use mod_deflate will be a better choice.
In addition, starting from Apache 2.0.45, mod_deflate can use DeflateCompressionLevel directive to set the compression level. The value of this directive can be an integer between 1 (fastest compression, lowest compression quality) to 9 (slowest compression, highest compression ratio). Its default value is 6 (compression speed and compression quality). a more balanced value). This simple change makes mod_deflate easily comparable to mod_gzip's compression.
P.S. For virtual spaces that do not have the above two Gzip modules enabled, you can also use PHP's zlib function library (you also need to check whether the server supports it) to compress files, but this method is more troublesome to use, and It generally consumes server resources, so please use it with caution according to the situation.
5. zlib.output_compression and ob_gzhandler encoding compression
The server does not support mod_gzip and mod_deflate modules. If you want to compress web page content through GZIP, you can consider two methods. Enable zlib.output_compression or encode through ob_gzhandler .
1) zlib.output_compression compresses the web content and sends data to the client at the same time.
2) ob_gzhandler waits for the web page content to be compressed before sending it. Compared with the former, it is more efficient. However, it should be noted that the two cannot be used at the same time. You can only choose one, otherwise an error will occur.
A brief description of the implementation of the two:
1. zlib.output_compression implementation
By default, zlib.output_compression is off:
The code is as follows:
If you need to edit the php.ini file to open it, add the following content:
You can check the result through the phpinfo() function.
When the Local of zlib.output_compression
When the values of Value and MasterValue are both On, it means that it has taken effect. The PHP page (including pseudo-static page) visited at this time has been GZIP compressed. Through Firebug or
The online web page GZIP compression detection tool can detect the compression effect.
2. Implementation of ob_gzhandler
If you need to use ob_gzhandler, you need to turn off zlib.output_compression and change the content of the php.ini file to:
zlib.output_compression = Off
zlib.output_compression_level = -1
By inserting in the PHP file Related code implements GZIP compression P compression:
No matter it is zlib.output_compression or ob_gzhandler, it can only perform GZIP compression on PHP files. For static files such as HTML, CSS, JS etc. Files can only be implemented by calling PHP.
The last thing I want to say is that the mainstream browsers now use the HTTP1.1 protocol by default.
All support GZIP compression. For IE, if you do not select its menu bar Tools-"Internet Options-"Advanced-"HTTP 1.1 Settings-"Use HTTP
1.1, then you will not feel the pleasure brought by the speed increase after web page compression!
http://www.bkjia.com/PHPjc/1056839.html