中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

php啟用gzip壓縮加速網(wǎng)站

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

說(shuō)明:

  1. 在服務(wù)器緩存了壓縮過(guò)的文件,再次訪問(wèn)減少再壓縮時(shí)間,降低CPU占用率。
  2. 通過(guò)設(shè)置客戶端文件緩存時(shí)間,降低再次請(qǐng)求次數(shù),可降低85%以上。
  3. 圖片因?yàn)橐呀?jīng)是壓縮格式,只是設(shè)置客戶端緩存時(shí)間,不做壓縮處理。

使用方法:

  1. 服務(wù)器必須支持gzip,Rewrite功能。
  2. 在.htacess文件的“RewriteBase /”下面一行添加下面的代碼 RewriteRule (..css$|..js$|..jpg$|..gif$|.*.png$) gzip.php?$1
  3. 上傳gzip.php到根目錄

4,在根目錄建cache文件夾,保證可讀寫。

<?php
/**
* @author Seraphim
* @copyright 2012
*/
// <!-- 公共的返回header的子程序 -->
function sendheader($last_modified, $p_type, $content_length = 0)
{
  // 設(shè)置客戶端緩存有效時(shí)間
  header("Expires: " . gmdate("D, d M Y H:i:s", time() + 15360000) . "GMT");
  header("Cache-Control: max-age=315360000");
  header("Pragma: ");
  // 設(shè)置最后修改時(shí)間
  header("Last-Modified: " . $last_modified);
  // 設(shè)置文件類型信息
  header($p_type);
  header("Content-Length: " . $content_length);
}
define('ABSPATH', dirname(__file__) . '/');
$cache = true;
$cachedir = 'cache/'; //存放gz文件的目錄,確保可寫
if (empty($_SERVER['QUERY_STRING']))
  exit();
$gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
if (empty($gzip))
  $cache = false;
$key = array_shift(explode('?', $_SERVER['QUERY_STRING']));
$key = str_replace('../', '', $key);
$filename = ABSPATH . $key;
$symbol = '_';
$rel_path = str_replace(ABSPATH, '', dirname($filename));
$namespace = str_replace('/', $symbol, $rel_path);
$cache_filename = ABSPATH . $cachedir . $namespace . $symbol . basename($filename) .
  '.gz'; //生成gz文件路徑
$ext = array_pop(explode('.', $filename)); //根據(jù)后綴判斷文件類型信息
$type = "Content-type: text/html"; //默認(rèn)的文件類型
switch ($ext)
{
  case 'css':
    $type = "Content-type: text/css";
    break;
  case 'js':
    $type = "Content-type: text/javascript";
    break;
  case 'gif':
    $cache = false;
    $type = "Content-type: image/gif";
    break;
  case 'jpg':
    $cache = false;
    $type = "Content-type: image/jpeg";
    break;
  case 'png':
    $cache = false;
    $type = "Content-type: image/png";
    break;
  default:
    exit();
}
if ($cache)
{
  if (file_exists($cache_filename))
  { // 假如存在gz文件
    $mtime = filemtime($cache_filename);
    $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
    if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && array_shift(explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) ==
      $gmt_mtime))
    {
      // 與瀏覽器cache中的文件修改日期一致,返回304
      header("HTTP/1.1 304 Not Modified");
      // 發(fā)送客戶端header
      header("Content-Encoding :gzip");
      sendheader($gmt_mtime, $type);
    }
    else
    {
      // 讀取gz文件輸出
      $content = file_get_contents($cache_filename);
      // 發(fā)送客戶端header
      sendheader($gmt_mtime, $type, strlen($content));
      header("Content-Encoding: gzip");
      // 發(fā)送數(shù)據(jù)
      echo $content;
    }
  }
  else
    if (file_exists($filename))
    { // 沒(méi)有對(duì)應(yīng)的gz文件
      $mtime = mktime();
      $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
      // 讀取文件
      $content = file_get_contents($filename);
      // 去掉空白的部分
      // $content = ltrim($content);
      // 壓縮文件內(nèi)容
      $content = gzencode($content, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);
      // 發(fā)送客戶端header
      sendheader($gmt_mtime, $type, strlen($content));
      header("Content-Encoding: gzip");
      // 發(fā)送數(shù)據(jù)
      echo $content;
      // 寫入文件
      file_put_contents($cache_filename, $content);
    }
    else
    {
      header("HTTP/1.0 404 Not Found");
    }
}
else
{ // 處理不使用Gzip模式下的輸出。原理基本同上
  if (file_exists($filename))
  {
    $mtime = filemtime($filename);
    $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
    if ((isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && array_shift(explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) ==
      $gmt_mtime))
    {
      // 與瀏覽器cache中的文件修改日期一致,返回304
      header("HTTP/1.1 304 Not Modified");
      // 發(fā)送客戶端header
      sendheader($gmt_mtime, $type, strlen($content));
      header("Content-Encoding :gzip");
    }
    else
    {
      // 讀取文件輸出
      $content = file_get_contents($filename);
      // 發(fā)送客戶端header
      sendheader($gmt_mtime, $type, strlen($content));
      // 發(fā)送數(shù)據(jù)
      echo $content;
    }
  }
  else
  {
    header("HTTP/1.0 404 Not Found");
  }
}
?>

標(biāo)簽: 代碼 服務(wù)器

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:php生成縮略圖代碼

下一篇:Python3.4圖片轉(zhuǎn)換素描