【问题标题】:Spreading server load with a mini CDN使用迷你 CDN 分散服务器负载
【发布时间】: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
  1. 这甚至会减少主服务器的负载吗?还是 file_get_contents 以这样一种方式工作,即主服务器最终“解析”(将文件放在一起以进行 html 输出)文件。

  2. 如何添加检查,以便如果子服务器离线而指针服务器不知道,我们会从主服务器提供静态 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();
}

所以我的第三个也是最后一个问题是;这种复制方法在资源方面是一种好方法吗?主服务器上的静态文件很少更改,只有在有人发表评论或编辑文章(或添加新文章)时才会更改,但资源节约仍然很重要。

【问题讨论】:

    标签: php cdn


    【解决方案1】:

    首先,我必须对出色演示文稿的答案进行投票。您的想法也不错,但仍然:回答您的问题:“不要这样做!”

    1. 如果 WP 插件配置为编写页面的实际 HTML,请先使用它们?例如。通过检查是否存在可以为当前请求提供服务的 html 文件来绕过整个 PHP 堆栈。 Apache、Nginx 或 Lighttpd 通过 URL 重写(甚至在 nginx 的核心)支持这一点。 [旁注:我不知道你使用的是哪个网络服务器或 Wordpress 插件,所以我省略了 config sn-ps。]

    2. 如果这还没有帮助,或者如果这还不够,请使用反向代理(如Varnish)分发内容。很容易将您的主服务器定义为源服务器,并让其前面的各种 Varnish 服务器处理其余的。

    3. 如果 Varnish 不够好,并且您的网站可以完全通过 CDN 提供服务,请查看像 edgecast 这样的人(speedyrails 是他们的经销商)。它们允许您进行透明缓存:

      • http://my-blog.com 直接指向edgecast的服务器
      • 您将 edgecast 配置为检查 http://mainserver.my-blog.com 是否没有填充缓存
      • 配置缓存标头以避免频繁查找
      • 它们还允许您清除缓存(在您更新博客时)
    4. 如果以上所有内容都不适合您,我建议您查看Cloudflare。他们做你想做的(以及更多)。我自己在试驾,到目前为止工作完美。

    【讨论】:

    • 这些都是非常好的技巧,谢谢。 3 和 4 对我个人来说是不可行的,因为自己做事更便宜(而且有趣)。 Varnish 看起来可能正是我正在寻找的东西,所以稍后再试一试。只是出于好奇,为什么“不要这样做!”?因为替代方案都更容易/防错,或者因为我的方法(file_get_contents)一开始就不会减少服务器负载?
    • Cloudflare 是免费的,顺便说一句。 (但也许并非总是如此。)
    • 对不起,回答为什么不。我认为出于各种原因:主服务器是您图片中的 SPOF。我认为这不会真正承担你想要的负载,因为 PHP 仍然被调用——无论如何。此外,来自其他服务器的 HTTP 请求会增加当前请求的延迟(几乎没有好处)。
    • 您是否有机会通过检查带有 url 重写的 html 来提供一个完全基于 php 的 Debian6 + Lighttpd 示例?我刚刚注意到,即使我让 wp_super_cache (插件)返回一个空白页面,当我同时连接 100 个客户端时,服务器负载仍然高达 100%。因为 php-fpm 进程还在制作中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多