Home > Article > Backend Development > How to implement php gzip compressed output_PHP tutorial
一、gzip介绍
gzip是GNU zip的缩写,它是一个GNU自由软件的文件压缩程序,也经常用来表示gzip这种文件格式。软件的作者是Jean-loup Gailly和Mark Adler。1992年10月31日第一次公开发布,版本号是0.1,目前的稳定版本是1.2.4。
Gzip主要用于Unix系统的文件压缩。我们在Linux中经常会用到后缀为.gz的文件,它们就是GZIP格式的。现今已经成为Internet 上使用非常普遍的一种数据压缩格式,或者说一种文件格式。 当应用Gzip压缩到一个纯文本文件时,效果是非常明显的,经过GZIP压缩后页面大小可以变为原来的40%甚至更小,这取决于文件中的内容。
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.
Here are two demonstration pictures:
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.
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: Copy the code The code is as follows:
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:
Configuration of mod_gzip:
# mod_gzip: 🎜> 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 example of mod_deflate:
Open
Open the apache configuration file httpd.conf Remove the #LoadModule deflate_module modules/mod_deflate.so from the beginning
Copy the code firebug查看: 注意: 1)不管使用mod_gzip 还是mod_deflate,此处返回的信息都一样。因为它们都是实现的gzip压缩方式。 2)CompressionLevel 9是指压缩程度的等级(设置压缩比率),取值范围在从1到9,9是最高等级。据了解,这样做最高可以减少8成大小的传输量(看档案内容而定),最少也能够节省一半。 CompressionLevel 预设可以采用 6 这个数值,以维持耗用处理器效能与网页压缩质量的平衡. 不建议设置太高,如果设置很高,虽然有很高的压缩率,但是占用更多的CPU资源. 四、mod_gzip 和mod_deflate的主要区别是什么?使用哪个更好呢? 第一个区别是安装它们的Apache Web服务器版本的差异: Apache 1.x系列没有内建网页压缩技术,所以才去用额外的第三方mod_gzip 模块来执行压缩。而Apache 2.x官方在开发的时候,就把网页压缩考虑进去,内建了mod_deflate 这个模块,用以取代mod_gzip。虽然两者都是使用的Gzip压缩算法,它们的运作原理是类似的。 第二个区别是压缩质量: mod_deflate 压缩速度略快而mod_gzip 的压缩比略高。一般默认情况下,mod_gzip 会比mod_deflate 多出4%~6%的压缩量。 那么,为什么使用mod_deflate? 第三个区别是对服务器资源的占用: 一般来说mod_gzip 对服务器CPU的占用要高一些。mod_deflate 是专门为确保服务器的性能而使用的一个压缩模块,mod_deflate 需要较少的资源来压缩文件。这意味着在高流量的服务器,使用mod_deflate 可能会比mod_gzip 加载速度更快。 不太明白?简而言之,如果你的网站,每天不到1000独立访客,想要加快网页的加载速度,就使用mod_gzip。虽然会额外耗费一些服务器资源, 但也是值得的。如果你的网站每天超过1000独立访客,并且使用的是共享的虚拟主机,所分配系统资源有限的话,使用mod_deflate 将会是更好的选择。 另外,从Apache 2.0.45开始,mod_deflate 可使用DeflateCompressionLevel 指令来设置压缩级别。该指令的值可为1(压缩速度最快,最低的压缩质量)至9(最慢的压缩速度,压缩率最高)之间的整数,其默认值为6(压缩速度和压缩质 量较为平衡的值)。这个简单的变化更是使得mod_deflate 可以轻松媲美mod_gzip 的压缩。 P.S. 对于没有启用以上两种Gzip模块的虚拟空间,还可以退而求其次使用php的zlib函数库(同样需要查看服务器是否支持)来压缩文件,只是这种方法使用起来比较麻烦,而且一般会比较耗费服务器资源,请根据情况慎重使用。 五、zlib.output_compression和ob_gzhandler编码方式压缩 服务器不支持mod_gzip、mod_deflate模块,若想通过GZIP压缩网页内容,可以考虑两种方式,开启zlib.output_compression或者通过ob_gzhandler编码的方式。 1)zlib.output_compression是在对网页内容压缩的同时发送数据至客户端。 2) ob_gzhandler waits for the web page content to be compressed before sending it. In comparison, the former is more efficient, but 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 turned off: When the Local Value and MasterValue of zlib.output_compression are both On, it means that it has taken effect. At this time, the accessed PHP page (including pseudo-static page) has been GZIP compressed. You can use Firebug or online web page GZIP compression detection tool. The effect of compression is detected. 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: The last thing I want to say is that the current mainstream browsers use the HTTP1.1 protocol by default, and basically 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!
The code is as follows:
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFL ATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE image/svg+xml
里面的文件MIME类型可以根据自己情况添加,至于PDF 、图片、音乐文档之类的这些本身都已经高度压缩格式,重复压缩的作用不大,反而可能会因为增加CPU的处理时间及浏览器的渲染问题而降低性能。所以就没必要再通过Gzip压缩。通过以上设置后再查看返回的HTTP头,出现以下信息则表明返回的数据已经过压缩。即网站程序所配置的Gzip压缩已生效。Content-Encoding: gzip
3) 对已经是压缩过的图片格式如jpg,音乐档案如mp3、压缩文件如zip之类的,就没必要再压缩了。
; 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 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 standard
; output_handler, or otherwise the output will be corrupted.
; http://php.net/zlib.output-compression
zlib.output_compression = Off
; http://php.net/zlib.output-compression-level
;zlib.output_compression_level = -1
If required Open the php.ini file to be edited and add the following content:
zlib.output_compression = On
zlib.output_compression_level = 6
You can check the result through the phpinfo() function.
2. Implementation of ob_gzhandler
zlib.output_compression = Off
zlib.output_compression_level = -1
By inserting in the PHP file Relevant code implements GZIP compression P compression:
if (extension_loaded('zlib')) {
//There is no page Output and the browser can accept the GZIP page
. > ob_end_flush();
No matter it is zlib.output_compression or ob_gzhandler, it can only perform GZIP compression on PHP files. Static files such as HTML, CSS, and JS can only be compressed by calling PHP.