【发布时间】:2011-10-31 00:59:10
【问题描述】:
为我的 wordpress 网站使用缓存插件可以大大减少服务器负载,但我正在尝试进一步减少它。缓存插件创建一个可以重复使用的静态 html 文件,而不是在每个请求中都用 php 重新创建它。
我想将此静态文件复制到多个服务器,以便可以从多个位置提供服务,从而分散服务器负载。
概念
我基本上是这样做的:
主服务器:
//Echo's what the pointer server will echo, which echo's what the sub-server echo's (which is the stored html file's content)
echo file_get_contents("http://$pointerserver?f=$fileNameWeAreRequesting";
指针服务器:
if(isset($_GET['f'])){ $requestedfile = $_GET['f']; }
/* Redirect user to file */
if(isset($_GET['redirect']))
{
//Filename example: file.html (shares same filename as it's original,
//but the content holds a string of sub-servers that host the file, seperated by a comma)
$servers = file_get_contents("filelocations/$requestedfile");
$pieces = explode(",", $servers); //Sub-Servers to array
//Check for online server
foreach ($pieces as $server)
{
if(file_get_contents("servercheck/$server") == "1") //Check if sub-server online
{
/* Echo file from sub-server NOW */
echo file_get_contents("http://$server?f=$requestedfile"); //Echo's whatever the sub-server echo's
break;
}
}
}
子服务器
/* Provide requested file */
echo file_get_contents("files/$requestedfile"); //Echo's the static html page stored on the sub-server
这甚至会减少主服务器的负载吗?还是 file_get_contents 以这样一种方式工作,即主服务器最终“解析”(将文件放在一起以进行 html 输出)文件。
如何添加检查,以便如果子服务器离线而指针服务器不知道,我们会从主服务器提供静态 html 文件。
让主服务器为此做好准备
缓存插件处理静态文件的制作,所以我想发送它:
主服务器:
//This doesn't send anything physical, just instructions to what file the pointer server needs to retrieve and then copy over to the sub-severs.
echo file_get_contents("http://$pointerserver?propagate&f=$fileNameWeAreRequesting";
指针服务器:(我知道这行不通,只是为了说明想法)
if(isset($_GET['f'])){ $requestedfile = $_GET['f']; }
/* Propagate file to sub-servers */
if(isset($_GET['propagate']))
{
if ($handle = opendir('subservers/')) //Retrieve all available sub-servers
{
while (false !== ($server = readdir($handle))) //For-each subserver, send the file
{
if ($server != "." && $server != "..")
{
/* CREATE AUTO-SUBMIT FORM HERE TO POST FILE CONTENTS (CONCEPT) */
action="http://$server/receivefile.php?f=$requestedfile&send"
$postThisStuff = file_get_contents("http://www.mainserver.xx/cache/$requestedfile"); //Reads the file contents from the main server so it can send it over to the sub-server
}
}
}
}
子服务器
/* Receive file */
if(isset($_GET['send']))
{
//Write received post date to file
$fh = fopen("files/$requestedfile", 'w') or die("can't open file");
$stringData = $_POST['thefile'];
fwrite($fh, $stringData);
fclose($fh);
die();
}
所以我的第三个也是最后一个问题是;这种复制方法在资源方面是一种好方法吗?主服务器上的静态文件很少更改,只有在有人发表评论或编辑文章(或添加新文章)时才会更改,但资源节约仍然很重要。
【问题讨论】: