页面静态化

页面静态化

如何为大型网站提速?
1、静态化
2、缓存技术memcache
3、mysql优化

学习内容:
1、页面静态化概念
2、页面静态化好处是什么
3、php如何实现页面静态化
4、页面静态化实例

在工作中遇到问题后的思路:提出问题->引出新技术->做个小案例(快速入门)->细节->如何使用到项目中去(为什么要用,怎么用)

三个重要的概念:
1、什么叫静态网址?纯静态HTML文档例如:http://localhost/test.html
2、什么叫动态网址?内容存于数据库中,根据要求显示内容,URL中以 ?, & 显示不同的参数例如:http://localhost/user.php?id=1
3、什么叫做伪静态网址?
伪静态仅仅是对动态网址的一个重写,伪静态网址不能让动态网址“静态化” ,搜索引擎不会认为伪静态就是HTML文档。
其次,伪静态可取,但应把重心放在去除冗余参数、规范URL、尽可能的避免重复页上。
例如:
这是一个动态网址 http://localhost/news.php?lang=cn&class=1&id=2
1、从seo的角度来看,最好重写为 http://localhost/news-cn-class1-id2.html。利于搜索引擎抓取
2、减少了sql注入攻击

什么是seo?(search engine optimization)搜索引擎优化

页面静态化分类?
1、真静态:把php->html,访问时不访问数据库
2、伪静态:只把网址静态,访问时还是访问数据库

面试题:为什么要页面要静态化或静态化必要性?
答:
1、apache在处理php的时候慢,处理html静态页面快一些。在apache/bin/ab.exe可做压力测试,该工具可以模拟并发访问某个页面,用来测页面速度好不好。
2、静态化利用seo优化(
        6个方面:
        1、url长度不得超过255字节。
        2、静态页面参数,做静态页面。
        3、meta信息完善程度,keywords和discription加进去。
        4、图片alt信息。
        5、frame信息,frame/frameset/iframe标签导致百度抓取困难
        6、flash文字信息,flash缺少文字描述
    )
3、防止SQL注入

面试题:真静态和伪静态的优点和缺点?
①真静态访问效率高,利于seo.可以减少对数据库的操作。但是会占用大量
的磁盘.
②伪静态一、可以方便的实现对搜索引擎的优化,二、占空间比较小。三、通过生成不同view-id2.hmtl 可以实现内容的变化.四有效的防止了注入攻击

使用方法:
1、打开黑窗口,运行->cmd
2、切换到你的apache/bin目录,例如:cd H:\PHP\xampp\apache\bin 或者 直接H:\PHP\xampp\apache\bin 或者 cd /d H:\PHP\xampp\apache\bin
3、ab.exe -n 10000 -c 100 http://localhost/test.php
参数:ab.exe -n (请求多少次) -c (表示多少人) 地址 瞬间并发
返回统计信息如下:

Server Software:        Apache/2.4.12 //apache版本
Server Hostname:        localhost //主机名
Server Port:            80 //端口号

Document Path:          /test1.php //测试的文件
Document Length:        5 bytes //文件长度

Concurrency Level:      100 //100个访问
Time taken for tests:   18.687 seconds //总共花费的时间
Complete requests:      10000 //访问次数
Failed requests:        0 //失败的请求数量
Total transferred:      4150000 bytes //整个场景中的网络传输量
HTML transferred:       50000 bytes //整个场景中的HTML内容传输量
Requests per second:    535.13 [#/sec] (mean) //每秒请求数
Time per request:       186.871 [ms] (mean) //每次请求的响应时间
Time per request:       1.869 [ms] (mean, across all concurrent requests)//每个访问响应时间
Transfer rate:          216.87 [Kbytes/sec] received //平均每秒网络上的流量,可以帮助排除是否存在网络流量过大导致响应时间延长的问题

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.5      0       8
Processing:    12  185 115.5    173    5395
Waiting:        9  185 115.5    173    5395
Total:         12  186 115.5    173    5396

//下面的内容为整个场景中所有请求的响应情况。在场景中每个请求都有一个响应时间
//其中 50% 的用户响应时间小于 173 毫秒,
//66% 的用户响应时间小于 186 毫秒,
//最大的响应时间小于 5396 毫秒
Percentage of the requests served within a certain time (ms)
  50%    173
  66%    186
  75%    200
  80%    210
  90%    235
  95%    258
  98%    303
  99%    366
 100%   5396 (longest request)

使用PHP缓存机制完成页面静态化

使用php自带的缓存机制来完成页面静态化,仅靠php自身的缓存机制并不能完美的解决页面静态化,往往需要和其它静态化技术(通常是伪静态技术)结合使用。
例子:当访问一个页面时,先判断是否存在缓存,如果存在,则直接输出缓存文件中的内容。否则,则先查询数据库,获得数据,然后生成缓存文件。
详解PHP的缓存机制(图), Output_buffering ,常用的函数包括ob_start() ob_get_contents() ,ob_clean(),ob_end_clean() ob_end_flush() ob_flush() flush()。

面试题:简述ob_flush() 和flush()的区别? 简述PHP缓存机制.

实例:
需求:对新闻访问标题,查看新闻内容,进行缓存静态化。
步骤:
1、创建测试数据库:

1 create table news(
2 id int unsigned primary key auto_increment,
3 title varchar(128) not null,
4 content varchar(256) not null,
5 filename varchar(32)) engine=MyISAM DEFAULT CHARSET=utf8;

2、初始化数据
insert into news (title,content) values('hello1','北京你好');
insert into news (title,content) values('hello2','世界你好');
3、创建文件:news_list.php
<?php
    $link = mysql_connect('localhost','root','') or die('数据库连接失败!');
    mysql_select_db('test',$link) or die("数据库选择失败!");
    mysql_set_charset('utf8',$link);
    $sql = 'SELECT * FROM news';
    $res = mysql_query($sql,$link);
    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
    echo "<h1>新闻列表</h1>";
    echo "<a href='#'>添加新闻</a><hr/>";
    echo "<table>";
    echo "<tr><td>id</td><td>标题</td><td>查看详情</td></tr>";
    while($row = mysql_fetch_row($res)){            
        echo "<tr><td>$row[0]</td><td>$row[1]</td><td><a href='show_news.php?id=$row[0]'>查看详情</a></td></tr>";
    }
    echo "</table>";
    //释放资源
    mysql_free_result($res);
    //关闭数据库
    mysql_close($link);
?>
4、创建文件:show_news.php

 1 <?php
 2     //接受id
 3     $id = $_GET['id'];
 4     $link = mysql_connect('localhost','root','') or die('数据库连接失败!');
 5     mysql_select_db('test',$link) or die("数据库选择失败!");
 6     mysql_set_charset('utf8',$link);
 7     $sql = "SELECT * FROM news WHERE id={$id}";
 8     $res = mysql_query($sql,$link);
 9     echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
10     if($row = mysql_fetch_assoc($res)){
11         echo "<table  border='1px' bordercolor='green' cellspacing='0' width=400px height=200px>";
12         echo "<tr><td>新闻详细内容</td></tr>";
13         echo "<tr><td>{$row['title']}</td></tr>";
14         echo "<tr><td>{$row['content']}</td></tr>";
15         echo "</table>";
16     }else{
17         header("content-type:text/html;charset=utf-8");
18         echo "<table  border='1px' bordercolor='green' cellspacing='0' width=400px height=200px>";
19         echo "<tr><td>新闻详细内容</td></tr>";
20         echo "<tr><td>没有数据</td></tr>";
21         echo "<tr><td>没有数据</td></tr>";
22         echo "</table>";
23     }
24     //释放资源
25     mysql_free_result($res);
26     //关闭数据库
27     mysql_close($link);    
28 ?>
29 5.创建缓存
30 <?php
31     //接受id
32     $id = $_GET['id'];
33     $link = mysql_connect('localhost','root','') or die('数据库连接失败!');
34     mysql_select_db('test',$link) or die("数据库选择失败!");
35     mysql_set_charset('utf8',$link);
36     $sql = "SELECT * FROM news WHERE id={$id}";
37     $res = mysql_query($sql,$link);
38     //开启ob缓存
39     ob_start();
40     echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
41     if($row = mysql_fetch_assoc($res)){
42         echo "<table  border='1px' bordercolor='green' cellspacing='0' width=400px height=200px>";
43         echo "<tr><td>新闻详细内容</td></tr>";
44         echo "<tr><td>{$row['title']}</td></tr>";
45         echo "<tr><td>{$row['content']}</td></tr>";
46         echo "</table>";
47     }else{
48         echo "<table  border='1px' bordercolor='green' cellspacing='0' width=400px height=200px>";
49         echo "<tr><td>新闻详细内容</td></tr>";
50         echo "<tr><td>没有数据</td></tr>";
51         echo "<tr><td>没有数据</td></tr>";
52         echo "</table>";
53     }
54     $html_content = ob_get_contents();
55     //构建文件名
56     $html_filename = "new_id" . $id . '.html';
57     //把ob->$html_filename(必要时需要考虑路径)
58     file_put_contents($html_filename,$html_content);
59     //释放资源
60     mysql_free_result($res);
61     //关闭数据库
62     mysql_close($link);    
63 ?>

6. 如何使用静态页面

 1 <?php
 2     //接受id
 3     $id = $_GET['id'];
 4     //构建文件名
 5     $html_filename = "new_id" . $id . '.html';
 6     //判断静态文件是否存在
 7     if(file_exists($html_filename)){
 8         //如果存在直接访问html页面
 9         echo '静态页面';
10         echo file_get_contents($html_filename);
11         exit();
12     }
13     $link = mysql_connect('localhost','root','') or die('数据库连接失败!');
14     mysql_select_db('test',$link) or die("数据库选择失败!");
15     mysql_set_charset('utf8',$link);
16     $sql = "SELECT * FROM news WHERE id={$id}";
17     $res = mysql_query($sql,$link);
18     //开启ob缓存
19     ob_start();
20     echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
21     if($row = mysql_fetch_assoc($res)){
22         echo "<table  border='1px' bordercolor='green' cellspacing='0' width=400px height=200px>";
23         echo "<tr><td>新闻详细内容</td></tr>";
24         echo "<tr><td>{$row['title']}</td></tr>";
25         echo "<tr><td>{$row['content']}</td></tr>";
26         echo "</table>";
27     }else{
28         echo "<table  border='1px' bordercolor='green' cellspacing='0' width=400px height=200px>";
29         echo "<tr><td>新闻详细内容</td></tr>";
30         echo "<tr><td>没有数据</td></tr>";
31         echo "<tr><td>没有数据</td></tr>";
32         echo "</table>";
33     }
34     $html_content = ob_get_contents();
35 
36     //把ob->$html_filename(必要时需要考虑路径)
37     file_put_contents($html_filename,$html_content);
38     //释放资源
39     mysql_free_result($res);
40     //关闭数据库
41     mysql_close($link);    
42 ?>

7.如果数据更新了,那静态页面什么更新?
解决方法1:判断静态页面是否过期(30s),如果过去重新生成。
代码实现:

 1 <?php
 2     //接受id
 3     $id = $_GET['id'];
 4     //构建文件名
 5     $html_filename = "new_id" . $id . '.html';
 6     //判断静态文件是否存在
 7     //函数:fielmtime()获取文件最后修改时间;
 8     if(file_exists($html_filename)&&filemtime($html_filename)+30>time()){
 9         //如果存在直接访问html页面
10         echo '静态页面';
11         echo file_get_contents($html_filename);
12         exit();
13     }
14     $link = mysql_connect('localhost','root','') or die('数据库连接失败!');
15     mysql_select_db('test',$link) or die("数据库选择失败!");
16     mysql_set_charset('utf8',$link);
17     $sql = "SELECT * FROM news WHERE id={$id}";
18     $res = mysql_query($sql,$link);
19     //开启ob缓存
20     ob_start();
21     echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
22     if($row = mysql_fetch_assoc($res)){
23         echo "<table  border='1px' bordercolor='green' cellspacing='0' width=400px height=200px>";
24         echo "<tr><td>新闻详细内容</td></tr>";
25         echo "<tr><td>{$row['title']}</td></tr>";
26         echo "<tr><td>{$row['content']}</td></tr>";
27         echo "</table>";
28     }else{
29         echo "<table  border='1px' bordercolor='green' cellspacing='0' width=400px height=200px>";
30         echo "<tr><td>新闻详细内容</td></tr>";
31         echo "<tr><td>没有数据</td></tr>";
32         echo "<tr><td>没有数据</td></tr>";
33         echo "</table>";
34     }
35     $html_content = ob_get_contents();
36 
37     //把ob->$html_filename(必要时需要考虑路径)
38     file_put_contents($html_filename,$html_content);
39     //释放资源
40     mysql_free_result($res);
41     //关闭数据库
42     mysql_close($link);    
43 ?>

解决方法2:
问题1:页面点击查看的时候,仍然是一个php页面(<a href='show_news.php?id=$row[0]'>查看详情</a>)不利于SEO;
问题2:实时性不够好,30秒延时;
解决方案:
当添加,更新新闻,就同步更新html静态页面,通常做法,设计模板文件,通过模板创建静态页面;
1.设计一个简单的模板文件;
2.增删改的时候通过模板文件,生成静态缓存文件,把名字存到服务器中。

伪静态:
1、如何配置apache rewrite功能
2、rewrite规则(正则)

1、检测apache是否支持mod_write.so;打开httpd.conf(默认情况是关闭的)
2、如果没有找到,那就手动添加一行:LoadModule rewrite module modules/mod_rewirte.so(必须独占一行)然后重启apache;
3、在httpd.conf配置虚拟主机#Virtual hosts打开,到c盘 hosts文件配置IP和域名的对应关系。
4、配置 httpd_vhost.comf 文件目录:apache\conf\extra\
<VirtualHost *:80>
    #ServerAdmin www.tp5.com #发邮件地址
    DocumentRoot "H:/PHP/xampp/htdocs/static" #文档根目录
    ServerName www.static.com
    ErrorLog "logs/dummy-host2.example.com-error.log"
    #CustomLog "logs/dummy-host2.example.com-access.log" common
    #配置rewrite相关信息
    <Directory "H:/PHP/xampp/htdocs/static">
    #允许所有
    Allow from all
    #拒绝所有请求
    #Deny from all
    #是否显示列表,如果虚拟主机在apache目录下则不生效。
    #Options+indexes
    #是否启用rewrite
    #Allowoverride all
    </Directory>
</VirtualHost>

在static下新建.htaccess文件(用工具建,windows默认不让建,Linux建立的是隐藏文件)

内容:
<IfModule rewrite_module>
RewriteEngine On
#规则可是多个,匹配从上到下
RewriteRule news-id(\d+)\.html$ news.php?id=$1
RewriteRule abc-id(\d+)\.html$ error.php
</IfModule>
重新访问www.static.com/news-id22.html 显示的是news.php的内容

有时候rewrite在httpd-vhosts.conf中直接配置例如:
<Directory "H:/PHP/xampp/htdocs/static">
#允许所有
Allow from all
#拒绝所有请求
#Deny from all
#是否显示列表(在发布项目后禁用的)
Options +indexes
#是否启用rewrite
#Allowoverride all
RewriteEngine On
RewriteRule news-id(\d+)\.html$ news.php?id=$1
</Directory>

问题:服务器,1个月生成1000w个静态html.

posted @ 2017-06-25 23:23  丶老中医  阅读(221)  评论(0编辑  收藏  举报
一切已经开始©2018 丶老中医