Apache PHP gzip Compression Output implementation method, apachegzip_php tutorial

Source: Internet
Author: User
Tags apache php

Apache PHP gzip Compression Output implementation method, Apachegzip


First, Gzip introduction

GZIP is the abbreviation for GNU Zip, which is a file compression program for GNU Free Software and is often used to represent the gzip file format. The authors of the software are Jean-loup gailly and Mark Adler. October 31, 1992 the first public release, the version number is 0.1, the current stable version is 1.2.4.

Gzip is primarily used for file compression on UNIX systems. We often use files with the. gz suffix in Linux, which are in gzip format. Nowadays it has become a very popular data compression format, or a file format, used on the Internet. When applying gzip compression to a plain text file, the effect is very noticeable, and after gzip compression the page size can change to 40% or smaller, depending on the content in the file.

GZIP encoding on the HTTP protocol is a technique used to improve the performance of Web applications. Web development can reduce the traffic on the website by gzip compression pages, and Gzip does not cause a lot of CPU usage, a little increase, but also a few percentage points, but for the page can be compressed more than 30%, very cost-effective.

Using the GZIP module in Apache, we can use the GZIP compression algorithm to compress the Web content published by the Apache server before transferring it to the client browser. This compression actually reduces the number of bytes transmitted over the network (saving the network I/O that is transmitted), and the most obvious benefit is that the speed of page loading can be accelerated.

The benefits of faster 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 retrieve pages faster than regular manual crawls by directly reading gzip files. As you can see in the Google Webmaster tool (Google Webmaster tools), sitemap.xml.gz is submitted directly as a sitemap.

While 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, plus other performance tuning mechanisms and the corresponding server-side caching rules, which can greatly improve the performance of the site. Therefore, for PHP programs that are deployed on Linux servers, we recommend that you turn on Use gzip Web compression when the server is supported.

Second, the Web server processing HTTP compression process is as follows:

1. After the Web server receives the HTTP request from the browser, check whether the browser supports HTTP compression (accept-encoding information);

2. If the browser supports HTTP compression, the Web server checks the suffix name of the requested file;

3. If the request file is an HTML, CSS and other static files, the Web server to the compression buffer directory to check whether the requested file has the latest compressed file;

4. If the compressed file of the requested file does not exist, the Web server returns the uncompressed request file to the browser and stores the compressed file of the requested file in the compressed buffer directory;

5. If the most recent compressed file of the requested file already exists, the compressed file of the requested file is returned directly;

6. If the request file is a dynamic file, the Web server dynamically compresses the content and returns to the browser, and the compressed content is not stored in the compressed cache directory.

Third, enable the Apache gzip feature

There are two types of modules that are compressed using the GZIP compression algorithm on Apache:Mod_gzip and Mod_deflate. To use gzip Web compression, first make sure that your server turns on support for one of these two components.

Although the use of Gzip also requires client browser support, but do not worry, most of the current browser has been supported gzip, such as IE, Mozilla Firefox, Opera, Chrome and so on.

By looking at the HTTP headers, we can quickly determine whether the client browser that is being used supports accepting gzip compression. If the following message appears in the HTTP header that you send, your browser supports accepting the appropriate gzip compression:

The code is as follows:
Accept-encoding:gzip Support Mod_gzip
Accept-encoding:deflate Support Mod_deflate
Accept-encoding:gzip,deflate supports both Mod_gzip and mod_deflate


As Firebug view:

Accept-encoding:gzip,deflate is supporting both Mod_gzip and mod_deflate

If the server has support for the Gzip component, then we can customize it in http.conf or. htaccess, here's a simple example of a. htaccess configuration:

Configuration of the Mod_gzip:

The code is as follows:

# Mod_gzip:

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.*


Configuration examples for mod_deflate:

Open the Apache configuration file httpd.conf

Will

#LoadModule Deflate_module modules/mod_deflate.so

#LoadModule Headers_module modules/mod_headers.so

Remove the # number from the beginning

The code is as follows:
# Mod_deflate:

Deflatecompressionlevel 6 #压缩率, 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


Inside the file MIME type can be added according to their own situation, as for the PDF, pictures, music documents, and so on itself are already highly compressed format, the role of repetitive compression, but may be due to increase CPU processing time and browser rendering problems to reduce performance. So there is no need to re-compress by gzip. With the above setting and then looking at the returned HTTP headers, the following information indicates that the returned data is already compressed. That is, the gzip compression configured by the Web site program is in effect.

Content-encoding:gzip

Firebug View:

Attention:

1) The information returned here is the same regardless of whether you use Mod_gzip or mod_deflate. Because they are all implementations of gzip compression methods.

2) CompressionLevel 9 refers to the level of compression (set compression ratio), the value range from 1 to 9, 9 is the highest level. It is understood that this can be reduced by up to 80% of the size of the transmission (depends on the contents of the file), at least to save half. The CompressionLevel preset can use a value of 6 to maintain a balance between processor performance and page compression quality. It is not recommended to set too high, if the setting is high, although there is a high compression rate, but consumes more CPU resources.
3) for already compressed image format such as JPG, music files such as MP3, compressed files such as zip, there is no need to compress.

Iv. What are the main differences between Mod_gzip and mod_deflate? Which is better to use?

The first difference is the difference in the version of the Apache Web server that installs them:

The Apache 1.x series does not have built-in Web page compression technology, so it uses additional third-party mod_gzip modules to perform compression. While the Apache 2.x official in the development of the time, the page compression into account, built a mod_deflate This module, to replace the mod_gzip. Although both are used by the GZIP compression algorithm, they operate in a similar principle.

The second difference is the compression quality:

Mod_deflate compression speed slightly faster and mod_gzip compression ratio is slightly higher. Generally, by default, Mod_gzip will be 4%~6% more compressed than mod_deflate.

So, why use mod_deflate?

The third difference is the use of server resources:

Generally speaking, Mod_gzip is a bit more expensive for server CPUs. Mod_deflate is a compression module specifically designed to ensure server performance, and mod_deflate requires less resources to compress files. This means that on high-traffic servers, using mod_deflate may load faster than Mod_gzip.

Don't understand? In short, if your site is less than 1000 independent visitors a day, and you want to speed up the loading of your pages, use Mod_gzip. While some additional server resources are worth it, it is worthwhile. If your site has more than 1000 independent visitors per day, and if you are using a shared virtual host, the mod_deflate will be a better choice if you have limited resources allocated to it.

Also, starting with Apache 2.0.45, mod_deflate can use the Deflatecompressionlevel directive to set the compression level. The value of this instruction can be 1 (the fastest compression, lowest compression quality) to 9 (the slowest compression speed, the highest compression rate) between the integer, the default value is 6 (compression speed and compression quality is more balanced value). This simple change makes the mod_deflate easily comparable to Mod_gzip compression.

P.S. For virtual spaces that do not have the above two gzip modules enabled, you can also fallback to use PHP's Zlib function library (also need to see if the server supports) to compress files, but this method is cumbersome to use, and generally consumes server resources, please use it as appropriate.

Five,zlib.output_compression and Ob_gzhandler encoding method Compression

The server does not support Mod_gzip, mod_deflate modules, if you want to compress the content of the Web page by gzip, you can consider two ways, open zlib.output_compression or through ob_ Gzhandler encoded in the same way .

1) zlib.output_compression is to send data to the client while compressing the content of the Web page.

2) Ob_gzhandler is waiting for the content of the page to be compressed before sending, compared to the former more efficient, but it is important to note that the two can not be used at the same time, can only choose one, otherwise there will be errors.

The two implementations are described in a simple way:

1. zlib.output_compression Realization Mode

By default, Zlib.output_compression is off:

The code is as follows:
; Transparent output compression using the Zlib library
; Valid values for this option is ' off ', ' on ', or a specific buffer size
; To being used for compression (default is 4KB)
; note:resulting chunk size may vary due to nature of compression. Php
; Outputs chunks that is few hundreds bytes each as a result of
; 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
; Output_handler, or otherwise the output would be corrupted.
; Http://php.net/zlib.output-compression
Zlib.output_compression = Off

; Http://php.net/zlib.output-compression-level
; zlib.output_compression_level = 1


To open the php.ini file you want to edit, add the following:

The code is as follows:
Zlib.output_compression = On
Zlib.output_compression_level = 6


The results can be detected by the Phpinfo () function.

When the value of Zlib.output_compression's local value and Mastervalue is the same, the representation is already in effect, and the PHP page (including the pseudo-static page) that was accessed is already gzip compressed, Compression can be detected using the Firebug or the online gzip compression detection tool.
2. How Ob_gzhandler is implemented

If you need to use Ob_gzhandler, you need to turn off zlib.output_compression and change the contents of the php.ini file to:
Zlib.output_compression = Off
Zlib.output_compression_level =-1
Gzip compression P compression is implemented by inserting the relevant code in the PHP 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)
Page with no output and the browser can accept Gzip
{
Ob_start (' Ob_gzhandler ');
}
}
What you want to compress
Echo $context;
Ob_end_flush ();


Whether it is zlib.output_compression or Ob_gzhandler, only the PHP files can be gzip compressed, for HTML, CSS, JS and other static files can only be done by invoking PHP.

Finally, the mainstream browser is now the default use of the HTTP1.1 protocol, the basic support gzip compression, for IE, if you do not select its menu bar tool-"Internet options-" Advanced-"HTTP 1.1 Settings-" Using HTTP 1.1, then, You will not feel the thrill of the speed increase after the page compression!

http://www.bkjia.com/PHPjc/1056839.html www.bkjia.com true http://www.bkjia.com/PHPjc/1056839.html techarticle Apache PHP gzip Compression Output implementation method, Apachegzip, Gzip introduction gzip is the GNU zip abbreviation, it is a GNU Free software file compression program, also often used to express gzip this article ...

  • 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.