网站一直在加载,很慢,处理方式多种;
0.页面静态化;
1.优化数据库;
2.负载均衡;
3.使用缓存技术
关于页面静态化
使用函数如 file_put_contents($filename,$string);
php的输出缓冲区 开启 5.3以后默认开启 output_bufferingon
没开启的话可以用 函数在页面开始 ob_start();
eg:
ob_start();
...//引文所需静态化的文件
file_put_contents('index.html',ob_get_contents());
ob_clean();
ob_start();
file_put_contents('index.html',ob_get_clean());
//注意ob_start() 无论php.ini 有没有开启 output_buffering 设置,
//最还都要开启,即使已经为on ,也是开辟新的输出缓冲区
关于如何触发系统生成纯静态页面
方式有三种:
0.页面添加缓存时间
用户访问如index.php 判断index.html的修改时间 如当前时间差值 若大于设定的数值学访问index.php否则访问静态页面
if(is_file('./index.html')&&time()-filemtime('./index.html')<300){
require './index.html';
}else{
ob_start();
......
file_put_contents('./index.html',ob_get_contents());
}
1.手动触发
同上不需要判断
2.定时任务 crontab
定时任务前几天转载了一篇博客关于linux定时任务的
http://my.oschina.net/u/2411815/blog/550833
局部静态化
<script>
$.ajax({
url:'xxx.php',
type:'get',
dataType:'json',
error:function(){
},
success:function(result){
if(result.code==1){
html='';
$.each(result.data,function(key,value){
html+='<li>'+value.title+'</li>'
})
$("#hot_html").html(html);
}else{
//todo
}
},
})
</script>
伪静态
<?php
/**
* 利用PHP正则表达式来处理伪静态
* 以http://static.com/newsList.php?type=2&category_id=1 => http://static.com/newsList.php/2/1.shtml
*/
//echo 12;
var_dump($_SERVER);
if(isset($_SERVER['PATH_INFO'])) {
// 解析 /2/1.shtml 匹配pathinfo值,如果没匹配到则数据不合法,若匹配到做相应处理
if(preg_match("/^\/(\d+)\/(\d+)(\.shtml)$/", $_SERVER['PATH_INFO'], $pathInfo)) {
//var_dump($pathInfo);
$type = $pathInfo[1]; // 类型值
$categoryId = $pathInfo[2]; // 所在栏目值
// 引入数据库链接类
require_once('./db.php');
$sql = "select * from news where `category_id` = ".$categoryId." and `type` = ".$type." order by id desc";
try{
$db = Db::getInstance()->connect();
$result = mysql_query($sql, $db);
$newsList = array();
while($row = mysql_fetch_assoc($result)) {
$newsList[] = $row;
}
var_dump($newsList);
exit;
}catch(Exception $e) {
// TODO
}
} else {
// TODO
die('url error!');
}
} else {
// TODO
// 错误提示,然后跳转到首页处理
die('地址有误');
}
路由重写
apache 开启重写模块
http.conf 中修改
LoadModule rewrite_module modules/mod_rewrite.so
Include conf/extra/httpd-vhosts.conf
在httpd-vhosts.conf中设置
<VirtualHost 127.0.0.19:80>
ServerName www.study.com
DocumentRoot "C:/wamp/www/study"
<Directory "C:/wamp/www/study">
Options Indexes
Order deny,allow
allow from all
</Directory>
#下面是重写时候若遇到目录文件下有该文件则显示文件
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
#下面是重写规则 //nignx重写类似
RewriteRule ^/detail/([0-9]*).html$ /detail.php?id=$1
</VirtualHost>
来源:oschina
链接:https://my.oschina.net/u/2411815/blog/596295