Linux gzip Compression output

Source: Internet
Author: User
Tags vars

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.

Here are two demo graphs:

gzip not used:

After opening using gzip:

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:

[CPP]View Plaincopyprint?
    1. Accept-encoding:gzip Support Mod_gzip
    2. Accept-encoding:deflate Support Mod_deflate
    3. 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:

[CPP]View Plaincopyprint?
    1. # Mod_gzip:
    2. <ifmodule mod_gzip.c>
    3. mod_gzip_on Yes
    4. Mod_gzip_dechunk Yes
    5. Mod_gzip_item_include file \. (html?| TXT|CSS|JS|PHP|PL) $
    6. Mod_gzip_item_include Handler ^cgi-script$
    7. Mod_gzip_item_include MIME ^text/.*
    8. Mod_gzip_item_include MIME ^application/x-javascript.*
    9. Mod_gzip_item_exclude Rspheader ^content-encoding:.*gzip.*
    10. <ifModule>

Configuration examples for mod_deflate:

Open the Apache configuration file httpd.conf

Remove the #loadmodule deflate_module modules/mod_deflate.so with the # number at the beginning

[CPP]View Plaincopyprint?
  1. # Mod_deflate:
  2. <ifmodule mod_deflate.c>
  3. Deflatecompressionlevel 6 #压缩率, 6 is the recommended value.
  4. Addoutputfilterbytype DEFLATE Text/plain
  5. Addoutputfilterbytype DEFLATE text/html
  6. Addoutputfilterbytype DEFLATE Text/xml
  7. Addoutputfilterbytype DEFLATE Text/css
  8. Addoutputfilterbytype DEFLATE Text/javascript
  9. Addoutputfilterbytype DEFLATE Application/xhtml+xml
  10. Addoutputfilterbytype DEFLATE Application/xml
  11. Addoutputfilterbytype DEFLATE Application/rss+xml
  12. Addoutputfilterbytype DEFLATE Application/atom_xml
  13. Addoutputfilterbytype DEFLATE Application/x-javascript
  14. Addoutputfilterbytype DEFLATE application/x-httpd-php
  15. Addoutputfilterbytype DEFLATE Image/svg+xml
  16. </ifmodule>

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.

[CPP]View Plaincopyprint?
    1. 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 to turn on zlib.output_compression or by Ob_gzhandler encoding .

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:

[CPP]View Plaincopyprint?
  1. ; Transparent output compression using the Zlib library
  2. ; Valid values for this option is ' off ', ' on ', or a specific buffer size
  3. ; To being used for compression (default is 4KB)
  4. ; note:resulting chunk size may vary due to nature of compression. Php
  5. ; Outputs chunks that is few hundreds bytes each as a result of
  6. ; Compression. If you prefer a larger chunk size for better
  7. ; Performance, enable output_buffering in addition.
  8. ; Note:you need to use Zlib.output_handler instead of the
  9. ; Output_handler, or otherwise the output would be corrupted.
  10. ; http://php.net/zlib.output-compression
  11. Zlib.output_compression = Off
  12. ; http://php.net/zlib.output-compression-level
  13. ; zlib.output_compression_level = 1

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

[CPP]View Plaincopyprint?
    1. Zlib.output_compression = On
    2. 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:

[CPP]View Plaincopyprint?
    1. Zlib.output_compression = Off
    2. Zlib.output_compression_level =-1


Gzip compression P compression is implemented by inserting the relevant code in the PHP file:

[CPP]View Plaincopyprint?
  1. if (extension_loaded (' zlib ')) {
  2. if (!headers_sent () and Isset ($_server[' http_accept_encoding ')) &&
  3. Strpos ($_server[' http_accept_encoding '), ' gzip ')!== FALSE)
  4. //page does not have output and the browser can accept gzip page
  5. {
  6. Ob_start (' Ob_gzhandler ');
  7. }
  8. }
  9. What you want to compress
  10. Echo $context;
  11. Ob_end_flush ();

How browser prompt: content encoding error, should be:

When using Ob_start (' Ob_gzhandler '), there is already a content output in front of it, checking the contents of the previous content and the require include call file. If you cannot find a way to use Ob_start () before calling another file, use Ob_end_clean () to clear the output after the call:

[PHP]View Plaincopyprint?
  1. if (extension_loaded (' zlib ')) {
  2. if (!headers_sent () and Isset ($_server[' http_accept_encoding ')) &&
  3. Strpos ($_server[' http_accept_encoding '), ' gzip ')!== FALSE)
  4. //page does not have output and the browser can accept gzip page
  5. {
  6. Ob_end_clean ();
  7. Ob_start (' Ob_gzhandler ');
  8. }
  9. }

Or we use Gzencode to compress:

[PHP]View Plaincopyprint?
    1. <?php
    2. $encoding = ' gzip ';
    3. $content = ' 123456789 ';
    4. Ob_end_clean ();
    5. Header (' content-encoding: '.   $encoding);
    6. $result = Gzencode ($content);
    7. echo $result;
    8. Exit

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!

Linux gzip Compression output

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