Implementation of apache php gzip compressed output, apachegzip
1. gzip Introduction
Gzip is short for GNU zip. It is a File compression program of GNU Free Software and is often used to indicate the file format gzip. Software is written by Jean-loup Gailly and Mark Adler. The first public release was made in October 31, 1992. The version number is 0.1, and the current stable version is 1.2.4.
Gzip is mainly used for File compression in Unix systems. We often use files suffixed with .gz in linux, which are in GZIP format. Nowadays, it has become a widely used data compression format on the Internet, or a file format. When Gzip is applied to a plain text file, the effect is very obvious. After GZIP compression, the page size can be changed to 40% or even smaller, depending on the content of the file.
GZIP encoding on HTTP 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. gzip does not occupy a large amount of cpu resources, but increases slightly by a few percent, however, it is very cost-effective to compress pages by more than 30%.
Using the Gzip module in Apache, we can use the Gzip compression algorithm to compress the webpage content published by the Apache server and then transmit it to the client browser. In this way, after compression, the number of bytes transmitted over the network is reduced (network I/o is saved). The most obvious advantage is that the speed of webpage loading can be accelerated.
The advantage of accelerated webpage loading is self-evident. In addition to saving traffic and improving the user's browsing experience, Gzip has a better relationship with search engine crawling tools. For example, Google can directly read gzip files to retrieve webpages faster than normal manual crawlers. As you can see in Google Webmaster toolsgz, sitemap.xml.gz is directly submitted as Sitemap.
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, adding other performance adjustment mechanisms and corresponding server-side cache rules can greatly improve the website performance. Therefore, for PHP programs deployed on Linux servers, we recommend that you enable Gzip Web compression with server support.
2. The Web server processes HTTP compression 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 request file;
3. If the requested file is a static file such as HTML and CSS, the Web server will check whether the latest compressed file of the requested file already exists in the compressed buffer directory;
4. If the compressed file of the request file does not exist, the Web server returns the uncompressed request file to the browser and stores the compressed file of the request file in the compressed buffer directory;
5. If the latest compressed file of the request file already exists, the compressed file of the request file will be directly returned;
6. If the request 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 compressed cache directory.
3. Enable the gzip function of apache
Apache uses the Gzip compression algorithm to compress modules: mod_gzip and mod_deflate. To use Gzip Web compression, first make sure that your server has enabled support for one of the two components.
Although Gzip also requires support from the client browser, do not worry. Currently, most browsers support Gzip, such as IE, Mozilla Firefox, Opera, and Chrome.
By viewing the HTTP header, we can quickly determine whether the client browser can accept gzip compression. If the following information appears in the HTTP header, your browser supports gzip compression:
The Code is as follows:
Accept-Encoding: gzip supports mod_gzip
Accept-Encoding: deflate supports mod_deflate
Accept-Encoding: gzip and deflate support both mod_gzip and mod_deflate.
For example, view firebug:
Accept-Encoding: gzip. deflate supports both mod_gzip and mod_deflate.
If the server enables support for Gzip components, you can customize them in http. conf or. htaccess. Below is a simple example of. htaccess Configuration:
Mod_gzip Configuration:
The Code is as follows:
# Mod_gzip:
<IfModule mod_gzip.c>
Mod_gzip_on Yes
Mod_gzip_dechunk Yes
Mod_gzip_item_include file \. (html? | Txt | css | js | php | pl) $
Mod_gzip_item_include handler ^ cgi-script $
Mod_gzip_item_include mime ^ text /.*
Mod_gzip_item_include mime ^ application/x-javascript .*
Mod_gzip_item_exclude rspheader ^ Content-Encoding:. * gzip .*
<IfModule>
Configuration instance of mod_deflate:
Open the apache configuration file httpd. conf.
Set
# LoadModule deflate_module modules/mod_deflate.so
# LoadModule headers_module modules/mod_headers.so
Remove the starting #
The Code is as follows:
# Mod_deflate:
<Ifmodule mod_deflate.c>
DeflateCompressionLevel 6 # compression ratio, 6 is the recommended value.
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xhtml + xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/rss + xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE image/svg + xml
</Ifmodule>
The file MIME type can be added according to your own situation. As for PDF files, images, music files, and other files, these files are already highly compressed, and repeated compression does not play a major role, however, the performance may be reduced by increasing the CPU processing time and rendering of the browser. Therefore, it is unnecessary to use Gzip compression. After the preceding settings, check the returned HTTP header. If the following information is displayed, the returned data has been compressed. That is, the Gzip compression configured by the website program takes effect.
Content-Encoding: gzip
View firebug:
Note:
1) Whether mod_gzip or mod_deflate is used, the information returned here is the same. Because they are all implemented gzip compression methods.
2) CompressionLevel 9 refers to the compression level (set the compression ratio). The value ranges from 1 to 9, and 9 is the highest level. It is understood that this can reduce the transmission volume by up to 8 characters (depending on the file content), and reduce the transmission volume by at least half. The CompressionLevel value can be set to 6 by default to maintain a balance between the consumed processor performance and the quality of Web Page compression. it is not recommended to set too high. If it is set very high, although there is a high compression rate, it occupies more CPU resources.
3) There is no need to compress a compressed image, such as jpg, a music file such as mp3, or a compressed file such as zip.
4. What are the main differences between mod_gzip and mod_deflate? Which one is better?
The first difference is the differences between the Apache Web Server versions that install them:
Apache 1.xThe series does not have built-in Web page compression technology, so we use an additional third-party mod_gzip module to perform compression. WhileApache 2.xDuring official development, we took into account web page compression and built the mod_deflate module to replace mod_gzip. Although both are Gzip compression algorithms, their operating principles are similar.
The second difference is the compression quality:
Mod_deflate compression speed is slightly faster, while mod_gzip compression ratio is slightly higher. By default, mod_gzip is 4% more than mod_deflate ~ 6% of the compression volume.
So why is mod_deflate used?
The third difference is the occupation of server resources:
In general, mod_gzip requires a higher CPU usage on the server. Mod_deflate is a compression module dedicated to ensuring server performance. mod_deflate requires a small amount of resources to compress files. This means that for high-traffic servers, using mod_deflate may be faster than loading mod_gzip.
Not quite clear? In short, if your website has less than 1000 independent visitors every day and you want to speed up webpage loading, use mod_gzip. Although it will consume some additional server resources, it is also worthwhile. If your website has more than 1000 independent visitors each day and you are using a shared virtual host and the allocated system resources are limited, using mod_deflate will be a better choice.
In addition, from Apache 2.0.45, mod_deflate can use the DeflateCompressionLevel command to set the compression level. The value of this command can be an integer between 1 (the fastest compression speed and the lowest compression quality) and 9 (the slowest compression speed and the highest compression ratio, the default value is 6 (the compression speed is balanced with the compression volume ). This simple change makes mod_deflate much easier than mod_gzip compression.
P.S. for virtual spaces without the preceding two Gzip modules enabled, you can use the zlib function library of php (also check whether the server supports this function) to compress files, this method is troublesome to use and generally consumes server resources. Please use it with caution as needed.
V,Zlib. output_compression andOb_gzhandler EncodingCompression Mode
The server does not support the mod_gzip and mod_deflate modules. You can use either of the following methods to compress webpage content through GZIP,Enable zlib. output_compression or use ob_gzhandler encoding..
1) zlib. output_compression compresses the webpage content and sends data to the client.
2) ob_gzhandler is sent only after the webpage content is compressed. In contrast, the former is more efficient, but note that the two cannot be used at the same time. You can only select one of them; otherwise, an error will occur.
The implementation methods of the two are described as follows:
1. zlib. output_compression Implementation Method
By default, zlib. output_compression is disabled:
The Code is as follows:
; Transparent output compression using the zlib library
; Valid values for this option are 'off', 'on', or a specific buffer size
; To be used for compression (default is 4KB)
; Note: Resulting chunk size may vary due to nature of compression. PHP
; Outputs chunks that are few hundreds bytes each as a result
; Compression. If you prefer a larger chunk size for better
; Performance, enable output_buffering in addition.
; Note: You need to use zlib. output_handler instead of the standard
; Output_handler, or otherwise the output will be upted.
Http://php.net/zlib.output-compression
Zlib. output_compression = Off
Http://php.net/zlib.output-compression-level
; Zlib. output_compression_level =-1
To enable the php. ini file, add the following content:
The Code is as follows:
Zlib. output_compression = On
Zlib. output_compression_level = 6
You can use the phpinfo () function to check the result.
When zlib. when the Local Value and MasterValue of output_compression are On, it indicates that they have taken effect. At this time, the accessed PHP page (including the pseudo static page) has been compressed by GZIP, the compression effect can be detected through Firebug or online web page GZIP compression detection tool.
2. Implementation of ob_gzhandler
To use ob_gzhandler, disable zlib. output_compression and change the php. ini file content:
Zlib. output_compression = Off
Zlib. output_compression_level =-1
Insert related code into the PHP file to compress the GZIP file:
The Code is as follows:
If (extension_loaded ('zlib ')){
If (! Headers_sent () AND isset ($ _ SERVER ['HTTP _ ACCEPT_ENCODING ']) &
Strpos ($ _ SERVER ['HTTP _ ACCEPT_ENCODING '], 'gzip ')! = FALSE)
// The page is not output and the browser can accept GZIP
{
Ob_start ('ob _ gzhandler ');
}
}
// Content to be compressed
Echo $ context;
Ob_end_flush ();
Both zlib. output_compression and ob_gzhandler can only compress PHP files. static files such as HTML, CSS, and JS files can only be implemented by calling PHP.
In the end, the mainstream browsers currently use the HTTP1.1 protocol by default, and basically all support GZIP compression. for IE, if you have not selected the menu bar tool-> Internet Options-> advanced-> HTTP 1.1 settings-> use HTTP 1.1, you will not feel the pleasure of Improving the compression speed of the web page!