简体   繁体   English

需要在2个负载平衡的服务器上上传单个文件

[英]Need to upload a single file across 2 load balanced servers

Currently my company has a 3 server set-up. 目前,我公司有3个服务器设置。 2 web boxes behind a load-balancer and another box not behind the load-balancer (used for Admin, CMS and stats). 负载均衡器后面有2个Web框,负载均衡器后面没有2个Web框(用于Admin,CMS和stats)。 Due to the state of funds at the moment we are looking to decommission our single box which is not behind the load-balancer. 由于目前的资金状况,我们希望停用不位于负载均衡器后面的单个设备。 The box has our CMS on it and a media subdomain points to /home/web/media on that box. 盒子上有我们的CMS,媒体子域指向该盒子上的/ home / web / media。 The problem is if we remove the box and port all the code (PHP) over to the load-balanced web boxes, then when a file is uploaded in the CMS it will only upload it to the media directory of the box the user hits. 问题是,如果我们删除该框并将所有代码(PHP)移植到负载平衡的Web框,那么在CMS中上传文件时,它只会将其上传到用户所点击框的媒体目录中。 So if a user hits web1 and uploads a file that file will only be accessible in the /home/web/media directory of web1. 因此,如果用户点击了web1并上传了文件,则该文件只能在web1的/ home / web / media目录中访问。 So we need to somehow rsync the /media directories on both web1 and web2 when a file is uploaded. 因此,当文件上传时,我们需要以某种方式rsync web1和web2上的/ media目录。 Or do something else. 或做其他事情。

What would you recommend to be the best way to accomplish this? 您会建议什么是实现此目标的最佳方法?

Any help would be much appreciated. 任何帮助将非常感激。

Just for information purposes we are running PHP 5.2, Red Hat Enterprise Linux and Apache 2.0.52 仅供参考,我们正在运行PHP 5.2,Red Hat Enterprise Linux和Apache 2.0.52

Regards, 问候,

Owen 欧文

You have a few choices (some have been already mentioned): 您有几种选择(已经提到过一些):

  • Store uploaded files in a database (not recommended for files you will need fast random access to). 将上载的文件存储在数据库中(不建议您需要快速随机访问的文件)。
  • Use a network filesystem such as NFS or SMB, and store uploaded files there. 使用网络文件系统(例如NFS或SMB),并将上传的文件存储在此处。 (You can also have code copy uploaded file to the other server's filesystem exposed over NFS or SMB). (您还可以将代码复制上载的文件复制到通过NFS或SMB公开的另一台服务器的文件系统中)。
  • Use a clustered filesystem such as GFS or OCFS. 使用群集文件系统,例如GFS或OCFS。

为媒体使用网络共享不是一个主意,因此您可以随时在两个服务器上都使用它吗?

function scp($username, $host, $port = 22, $file, $destination){

        $dirs = explode("/", $destination);
        array_pop($dirs);
        $dirs = implode("/", $dirs);

//         die("\nscp ".$file." ".$username."@".$host.":".$dirs);

         system("ssh ".$username."@".$host." mkdir -p ".$dirs);
         system("scp ".$file." ".$username."@".$host.":".$destination);
    }

of course you need that www-data has its public key on both servers and write privileges 当然,您需要www-data在两个服务器上都具有其公钥并具有写特权

If you want the file uploaded to both servers in realtime, just do that. 如果要将文件实时上传到两台服务器,只需执行此操作。

  1. User uploads a file on server1 . 用户将文件上传到server1
  2. server1 process the upload and stores it in server1/media . server1处理上载并将其存储在server1/media
  3. server1 makes an authenticated request to server2/api/uploadFile (curl) server1server2/api/uploadFile (curl)进行身份验证的请求
  4. server2 process the upload and stores it in server2/media . server2处理上载并将其存储在server2/media

Hope that makes sense. 希望有道理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM