页面静态化

2015.8.15

 
关于优化页面响应时间
方法:
1.动态页面静态化
2.优化数据库
3.使用负载均衡
4.使用缓存
动态页面静态化
如果页面中的一些内容不经常改动,动态页面静态化是非常有效的加速方法。
实质:生成静态的HTML文件
好处:减少服务器脚本的计算时间,降低服务器的响应时间,减少服务器压力
说明:不适用内容经常变化的应用 如:微博
 
关于动态URL地址设置静态形式(伪静态)
 
静态化介绍
 
php静态化:纯静态(局部纯静态,全部纯静态),伪静态
 
纯静态化案例实现
 
buffer认知
配置:http://php.net/output-buffering
buffer就是一个缓冲区,一个内存地址空间,主要用来存储内容
 
若开启缓冲区便可以将数据先进过buffer缓冲区
开启方式:在php.ini中修改output_buffering=on
或者在php代码中使用ob_start();函数
ob_get_contents();//获取缓冲区内容
 
php如何实现页面纯静态化
 
基本方式:使用file_put_contents()函数
使用php内置缓存机制实现页面静态化 -output_buffering
 
OB函数
ob_start();打开输出控制缓冲
ob_get_contents();返回数据缓冲区内容
ob_clean();清空数据缓冲区
ob_get_clean();得到当前缓冲区的内容并删除当前输出缓冲区
 
实现方式:使用ob函数获得缓冲内容,再使用file_put_contents()写入html文件
 
1.链接数据库去的数据
2.将获取的数据填充到模板文件中
3.需要把动态页面转为静态页面,生成静态化文件
 
第三步:
  1. ob_start();
  2. require_once('./templates/index.htm');
  3. if(file_put_contents('index.html',ob_get_clean())){
  4. echo 'success';
  5. }
  6. else{
  7. echo 'error';
  8. }
如mycms
 
如何触发系统生成纯静态化页面
模板文件关键代码(模板文件虽然定为.php文件,但是是html的格式)
  1. <divclass="plan_1"><imgsrc="/template/public/image/plan.png">新闻条目XXXXX</div><!--项目目录-->
  2. </div>
  3. <divstyle="width:325px;float:right; margin-top:10px;">
  4. <?php foreach($data as $k=>$v){?>
  5. <dlclass="Grey"style="background-image:url(/template/public/image/1yellow.png)">
  6. <dtstyle="margin-left:70px; padding-top:10px;"><?php echo $v['title']?></dt>
  7. <ddstyle="margin-left:70px"><?php echo $v['content']?></dd>
  8. </dl>
  9. <?php }?>
  10. </div>
1.页面添加缓存时间
  1. <?php
  2. //filemtime()获取文件的最近修改时间 判断文件是否有效300秒
  3. if(is_file('./index.html')&&(time()-filemtime('./index.html'))<300){
  4. require_once('./index.html');//直接显示该静态页面
  5. }else{
  6. require_once('./db.php');//获取数据类
  7. $connect=Db::getInstance()->connect();
  8. $sql="select * from news order by id desc";
  9. $result=mysql_query($sql,$connect);
  10. $news=array();
  11. while($row = mysql_fetch_array($result)){
  12. $news[]=$row;
  13. }
  14. //开启缓冲区
  15. ob_start();
  16. //引入模板文件
  17. require_once('./templates/index.php');
  18. file_put_contents('./index.html'ob_get_contents());
  19. }
这样访问index.php便会访问到静态页面
 
2.手动触发方式
直接调用控制器方法生成静态页面
2015.8.17
3.crontab定时扫描程序
 
1.使用putty链接上服务器,
2.使用crontab -e命令修改crontab文件
3.输入*/1 * * * * php /data/state/index.php (每一分钟执行index.php的程序)
 
局部动态化案例实现
静态化页面中如果想加载动态的内容如何处理。。
ajax技术
 
伪静态
 
php处理伪静态
 
path_info模式,(nginx服务器默认不支持path_info模式,需要配置才可以)
  1. <?php
  2. /*
  3. 通过正则表法师去分析伪静态URL地址
  4. http://state.com/newList.php?type=2&category_id=1
  5. 转为 http://state.com/newList.php/2/1.html
  6. */
  7. // /2/1.html
  8. if(preg_match('/^\/(\d+)\/(\d+).html/',$_SERVER['PATH_INFO'],$arr)){
  9. $type=$arr[1];
  10. $category_id=$arr[2];
  11. }else{
  12. //todo
  13. }
  14. print_r($arr);
  15. //print_r($_SERVER);
 
WEB服务器rewrite配置以及案例
 
apache下rewirte配置
 
1.虚拟域名配置
    1.在httpd.conf文件中开启相关模式
        LoadModule rewrite module modules/mod_rewrite.so
        Include conf/extra/httpd-vhost.conf
    2.httpd_vhosts.conf配置文件配置相关信息
        该vhost文件是在上面中配置好的conf文件
        然后在C:\Windows\System32\drivers\etc 中的host文件中便配置好的虚拟域名
  2.伪静态配置
        在vhost中,在虚拟域名下面加入
       在虚拟域名的根标签
 
        RewriteEngine on
       RewriteRule ^/test_wei/([0-9]*).html$ /test_wei.php?id=$1nginx下rewrite配置
 
        再此,若该目录下有test_wei/1.html的静态页面时,访问却是会访问到伪静态页面
        所以还需要配置,一下信息,使若有静态页面,先访问到静态页面
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
        RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
 
nginx下rewrite配置
 
rewirte ^/test_wei/(\d+)\.html$ /test_wei.php?id=$1 last;

 



posted on 2017-05-09 11:36  轻浮不韪  阅读(209)  评论(0编辑  收藏  举报