- 注册时间
- 2008-3-15
- 在线时间
- 128 小时
- 阅读权限
- 200
- 积分
- 0
- 帖子
- 205
- 精华
- 0
- UID
- 1
  
|
http://blog.soaper.asia
解決gizp 的問題
WordPress 2.5 今天出了正式版,我已经成功升级到了2.5版本。
上较之前版本2.3有了大面积的代码重构。特别的是后台管理的交互界面,有了全新的设计。
更强的Ajax后台管理,加上更清晰的交互界面,享受吧,更多的发现赶快开始体验吧。
PS:由于这次升级改动很大,对于使用到一些特殊功能的插件可能会不能正常运行。
发现2.5没有启用gzip的选项,默认也没有启用,其实只需手动在index.php中找到define(’WP_USE_THEMES’, true);这一行,在后面上ob_start(’ob_gzhandler’);就可以启用gzip了。
较充分的判断处理代码:
if(ereg('gzip',$_SERVER['HTTP_ACCEPT_ENCODING'])){//判断浏览器是否支持Gizp
if(substr($_SERVER['REQUEST_URI'],0,10)!='/download/')//排除不需要Gzip压缩的目录
ob_start('ob_gzhandler');//打开Gzip压缩
}
提前完成手上的事情,来总结一下前不久给自己小站优化心得。
gzip压缩功能在wordpress 2.3时代已经使用得普及了,好处很明显,而且99%的浏览器都支持以gzip模式压缩的网页。压缩率也非常的高,可达60%~90%,对于优化网页下载速度是非常见效的。(但是2.5版本开始没有这项功能了,如何手动开启?)
但是,由于是压缩就会耗费服务器的CPU资源,所以在CPU使用高的时期,Gzip压缩模式下的网页访问速度可能比没有压缩的网页还要慢。
这个就是所谓的时间换空间的概念了。我想,要是能解决了每次访问都要CPU来压缩的问题,直接从压缩文件来输出那就可以弥补这一矛盾的问题了。
我想到了先对页面引用的css和js文件。这些文件在页面引用时默认就是以静态文本文件传输。没有Gzip过。对于这样的文本型文件,如果做了压缩了,将有极大的压缩空间,极大减少流量,明显的减少传输时间。
来实际的了,在根目录的.htaccess里面加上RewriteRule (.*.css$|.*.js$) gzip.php?$1 [L]
这样可以让所有的css和js文件访问就以相对根目录的路径以GET变量传递到了gzip.php,交给gzip.php来全权处理了。(这里实在是很感激rewrite的强大!无须改动其他的主题文件来实现功能。)
gzip.php的代码如下:
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 | <?php define('ABSPATH', dirname(__FILE__).'/'); $cache = true;//Gzip压缩开关$cachedir = 'wp-cache/';//存放gz文件的目录,确保可写 $gzip = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');$deflate = strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate');$encoding = $gzip ? 'gzip' : ($deflate ? 'deflate' : 'none'); if(!isset($_SERVER['QUERY_STRING'])) exit(); $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文件路径 $type="Content-type: text/html"; //默认的类型信息 $ext = array_pop(explode('.', $filename));//根据后缀判断文件类型信息 switch ($ext){ case 'css': $type="Content-type: text/css"; break; case 'js': $type="Content-type: text/javascript"; 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"); header("Expires: "); header("Cache-Control: "); header("Pragma: "); header($type); header("Tips: Cache Not Modified (Gzip)"); header ('Content-Length: 0'); }else{ //读取gz文件输出 $content = file_get_contents($cache_filename); header("Last-Modified:" . $gmt_mtime); header("Expires: "); header("Cache-Control: "); header("Pragma: "); header($type); header("Tips: Normal Respond (Gzip)"); header("Content-Encoding: gzip"); echo $content; } }else if(file_exists($filename)){ //没有对应的gz文件 $mtime = mktime(); $gmt_mtime = gmdate('D, d M Y H:i:s', $mtime) . ' GMT'; $content = file_get_contents($filename);//读取文件 $content = gzencode($content, 9, $gzip ? FORCE_GZIP : FORCE_DEFLATE);//压缩文件内容 header("Last-Modified:" . $gmt_mtime); header("Expires: "); header("Cache-Control: "); header("Pragma: "); header($type); header("Tips: Build Gzip File (Gzip)"); header ("Content-Encoding: " . $encoding); header ('Content-Length: ' . strlen($content)); echo $content; if ($fp = fopen($cache_filename, 'w')) {//写入gz文件,供下次使用 fwrite($fp, $content); fclose($fp); } }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) ){ header ("HTTP/1.1 304 Not Modified"); header("Expires: "); header("Cache-Control: "); header("Pragma: "); header($type); header("Tips: Cache Not Modified"); header ('Content-Length: 0'); }else{ header("Last-Modified:" . $gmt_mtime); header("Expires: "); header("Cache-Control: "); header("Pragma: "); header($type); header("Tips: Normal Respond"); $content = readfile($filename); echo $content; } }else{ header("HTTP/1.0 404 Not Found"); }} ?> |
gzip.php (1.3 KiB, 54 hits)
启用gzip压缩模式传输
现在我这种Gizp处理也算是静态化处理,对于静态的问题。会带来好处,也有坏处,对于一些文章的访问统计和评论页面如果静态化了貌似就不能正常工作了。对与js和css文件的Gzip压缩方面,用了这段时间觉得还满意。
还有些其他的需要总结,现在想到的有:
WordPress中的一些常用函数的输出如何cache到文件,使其不再每次都请求数据库的查询、耗费CPU计算,也是直接从cache中读取,还有清理cache的管理页面,这些下次再写。
出處 http://www.cbmland.com/post/522/optimized-wordpress-notes-1.html |
|