1.在haproxy端写入读写分离策略
cd /etc/haproxy
vim haproxy.cfg
66 # 读参数:
67 acl read method GET ##两个read write 只用一个就行
68 acl read method HEAD
69 # 写参数:
70 acl write method PUT
71 acl write method POST
use_backend app if write #如果写就访问后端动态服务器
default_backend static #默认访问跳转到静态服务器
backend static
server web2 172.25.7.3:80 check
backend app #动态
server web1 172.25.7.2:80 check
server local 172.25.7.1:8000 backup #备机
2.在server2和server3中
给server2和server3安装php : yum install php -y
在默认发布目录下放入index.php(选择图片的静态页面)和upload_file.php(上传图片的动态页面),存放上传图片的目录upload目录。
cd /var/www/html
mkdir upload
chmod 777 upload #给server2和server3的默认上传目录权限
index.php内容:
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
upload_file.php内容:
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
重启haproxy:
systemctl restart haproxy
3.测试
step1 访问http://172.25.254.1/index.php
step2 上传一张图片
在server2的upload中可以看到该图片,而server3中没有
只是因为server2是处理写的后端服务器,server3只是读取,所以写入的东西保存在了2上,三没有。