apache/php 開啓 gzip壓縮

 

一、php方式開啓php

原理:css

header("Content-Encoding: gzip");
echo gzencode('songjiankang');

 示例1:html

function ob_gzip ($content) // $content 就是要壓縮的頁面內容,或者說餅乾原料
{
    if (! headers_sent() &&     // 若是頁面頭部信息尚未輸出
    extension_loaded("zlib") &&     // 並且zlib擴展已經加載到PHP中
    strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip"))     // 並且瀏覽器說它能夠接受GZIP的頁面
    {
        $content = gzencode($content . " \n//此頁已壓縮", 9); // 此頁已壓縮」的註釋標籤,而後用zlib提供的gzencode()函數執行級別爲9的壓縮,這個參數值範圍是0-9,0表示無壓縮,9表示最大壓縮,固然壓縮程度越高越費CPU。
                                                                  
        // 而後用header()函數給瀏覽器發送一些頭部信息,告訴瀏覽器這個頁面已經用GZIP壓縮過了!
        header("Content-Encoding: gzip");
        header("Vary: Accept-Encoding");
        header("Content-Length: " . strlen($content));
    }
    return $content; // 返回壓縮的內容,或者說把壓縮好的餅乾送回工做臺。
}

ob_start('ob_gzip');

 

 示例2:apache

#ob_gzhandler 爲php內置函數,具體參考手冊
ob_start('ob_gzhandler');

 

二、apache的方式開啓:json

a.開啓模塊:瀏覽器

LoadModule deflate_module modules/mod_deflate.so
LoadModule headers_module modules/mod_headers.so

 

b.httpd.conf中增長服務器

<ifmodule deflate_module>
  DeflateCompressionLevel 9
  AddOutputFilterByType DEFLATE text/html text/plain text/xml application/json application/xml
  AddOutputFilter DEFLATE js css
  AddOutputFilter INCLUDES .shtml .htm .xml .php .html
</ifmodule>

c.重啓服務器app

相關文章
相關標籤/搜索