欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

在php虚拟主机中启用gzip压缩的方法

程序员文章站 2022-05-16 19:36:53
...
  1. define('ABSPATH', dirname(__FILE__).'/');

  2. $cache = true;//Gzip压缩开关

  3. $cachedir = 'gzip_cache/';//存放gz文件的目录,使用前创建,并赋予可写权限
  4. $gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');

  5. $deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');
  6. $encoding = $gzip ? 'gzip' : ($deflate ? 'deflate' : 'none');
  7. if(!isset($_SERVER['QUERY_STRING'])) exit();

  8. $key=array_shift(explode('?', $_SERVER['QUERY_STRING']));

  9. $key=str_replace('../','',$key);
  10. $filename=ABSPATH.$key;
  11. $symbol='^';
  12. $rel_path=str_replace(ABSPATH,'',dirname($filename));
  13. $namespace=str_replace('/',$symbol,$rel_path);
  14. $cache_filename=ABSPATH.$cachedir.$namespace.$symbol.basename($filename).'.gz';//缓存路径
  15. $type="Content-type: text/html"; //MIME信息
  16. $ext = array_pop(explode('.', $filename));//获取文件扩展名
  17. switch ($ext){//更新MIME信息

  18. case 'css':
  19. $type="Content-type: text/css";
  20. break;
  21. case 'js':
  22. $type="Content-type: text/javascript";
  23. break;
  24. default:
  25. exit();
  26. }
  27. if($cache){

  28. if(file_exists($cache_filename)){//假如存在gz文件
  29. $mtime = filemtime($cache_filename);
  30. $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
  31. if( (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
  32. array_shift(explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) == $gmt_mtime)
  33. ){
  34. // 若文件无变动,返回304
  35. header ("HTTP/1.1 304 Not Modified");
  36. header("Expires: ");
  37. header("Cache-Control: ");
  38. header("Pragma: ");
  39. header($type);
  40. header("Tips: Cache Not Modified (Gzip)");
  41. header ('Content-Length: 0');
  42. }else{

  43. //读取gz文件输出

  44. $content = file_get_contents($cache_filename);
  45. header("Last-Modified:" . $gmt_mtime);
  46. header("Expires: ");
  47. header("Cache-Control: ");
  48. header("Pragma: ");
  49. header($type);
  50. header("Tips: Normal Respond (Gzip)");
  51. header("Content-Encoding: gzip");
  52. echo $content;
  53. }
  54. }else if(file_exists($filename)){ //没有对应的gz文件

  55. $mtime = mktime();

  56. $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
  57. $content = file_get_contents($filename);
  58. $content = gzencode($content, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);//压缩内容
  59. header("Last-Modified:" . $gmt_mtime);
  60. header("Expires: ");
  61. header("Cache-Control: ");
  62. header("Pragma: ");
  63. header($type);
  64. header("Tips: Build Gzip File (Gzip)");
  65. header ("Content-Encoding: " . $encoding);
  66. header ('Content-Length: ' . strlen($content));
  67. echo $content;
  68. if ($fp = fopen($cache_filename, 'w')) {//写入缓存

  69. fwrite($fp, $content);
  70. fclose($fp);
  71. }
  72. }else{

  73. header("HTTP/1.0 404 Not Found");
  74. }
  75. }else{ //关闭Gzip压缩
  76. //by bbs.it-home.org
  77. if(file_exists($filename)){
  78. $mtime = filemtime($filename);
  79. $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
  80. if( (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
  81. array_shift(explode(';', $_SERVER['HTTP_IF_MODIFIED_SINCE'])) == $gmt_mtime)
  82. ){
  83. header ("HTTP/1.1 304 Not Modified");
  84. header("Expires: ");
  85. header("Cache-Control: ");
  86. header("Pragma: ");
  87. header($type);
  88. header("Tips: Cache Not Modified");
  89. header ('Content-Length: 0');
  90. }else{
  91. header("Last-Modified:" . $gmt_mtime);
  92. header("Expires: ");
  93. header("Cache-Control: ");
  94. header("Pragma: ");
  95. header($type);
  96. header("Tips: Normal Respond");
  97. $content = readfile($filename);
  98. echo $content;
  99. }
  100. }else{
  101. header("HTTP/1.0 404 Not Found");
  102. }
  103. }
  104. ?>
复制代码

接着,在.htaccess(Apache mod_rewrite)或httpd.ini(IIS ISAPI_Rewrite)中添加以下规则:

  1. RewriteRule (.*.css$|.*.js$) /gzip.php\?$1 [L]
复制代码

最后,进行测试。 访问网站的各个页面,看看gzip_cache文件夹中是否有缓存文件生成。 也可以用百度站长工具,可以看到css/js页面是否压缩。