Linux gzip compressed output and linuxgzip compressed output

Source: Internet
Author: User
Tags website performance

Linux gzip compressed output and linuxgzip compressed output

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.

The following are two demo images:

Gzip not used:

 

After Gzip is Enabled:

 

 

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:

[Cpp]View plaincopyprint?
  1. Accept-Encoding: gzip supports mod_gzip
  2. Accept-Encoding: deflate supports mod_deflate
  3. 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:

[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 instance of mod_deflate:

 

Open the apache configuration file httpd. conf.

Remove # At the beginning of # LoadModule deflate_module modules/mod_deflate.so

[Cpp]View plaincopyprint?
  1. # Mod_deflate:
  2. <Ifmodule mod_deflate.c>
  3. DeflateCompressionLevel 6 # compression ratio, 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>

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.

[Cpp]View plaincopyprint?
  1. 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:

 

[Cpp]View plaincopyprint?
  1. ; Transparent output compression using the zlib library
  2. ; Valid values for this option are 'off', 'on', or a specific buffer size
  3. ; To be used for compression (default is 4KB)
  4. ; Note: Resulting chunk size may vary due to nature of compression. PHP
  5. ; Outputs chunks that are few hundreds bytes each as a result
  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 standard
  9. ; Output_handler, or otherwise the output will be upted.
  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 enable the php. ini file, add the following content:

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

[Cpp]View plaincopyprint?
  1. Zlib. output_compression = Off
  2. Zlib. output_compression_level =-1


Insert related code into the PHP file to compress the GZIP 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. // The page is not output and the browser can accept GZIP
  5. {
  6. Ob_start ('ob _ gzhandler ');
  7. }
  8. }
  9. // Content to be compressed
  10. Echo $ context;
  11. Ob_end_flush ();

 

 

How to prompt in the browser: The Content Encoding Error should be:

When ob_start ('ob _ gzhandler ') is used, the preceding content is output. Check the preceding content and the content of the require include call file. If it cannot be found, you can use ob_start () before calling other files, and then use ob_end_clean () to clear the output content:

 

[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. // The page is not output and the browser can accept GZIP
  5. {
  6. Ob_end_clean ();
  7. Ob_start ('ob _ gzhandler ');
  8. }
  9. }

Or we can use gzencode to compress:

 

 

[Php]View plaincopyprint?
  1. <? Php
  2. $ Encoding = 'gzip ';
  3. $ Content = '000000 ';
  4. Ob_end_clean ();
  5. Header ('content-Encoding: '. $ encoding );
  6. $ Result = gzencode ($ content );
  7. Echo $ result;
  8. Exit;

 

 

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!

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.