简体   繁体   中英

Need to upload a single file across 2 load balanced servers

Currently my company has a 3 server set-up. 2 web boxes behind a load-balancer and another box not behind the load-balancer (used for Admin, CMS and 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. 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. So if a user hits web1 and uploads a file that file will only be accessible in the /home/web/media directory of web1. So we need to somehow rsync the /media directories on both web1 and web2 when a file is uploaded. 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

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. (You can also have code copy uploaded file to the other server's filesystem exposed over NFS or SMB).
  • Use a clustered filesystem such as GFS or 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

If you want the file uploaded to both servers in realtime, just do that.

  1. User uploads a file on server1 .
  2. server1 process the upload and stores it in server1/media .
  3. server1 makes an authenticated request to server2/api/uploadFile (curl)
  4. server2 process the upload and stores it in server2/media .

Hope that makes sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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