HAProxy——讀寫分離
1. 在haproxy端寫入讀寫分離策略
cd /etc/haproxy
vim haproxy.cfg
66 # 讀參數:
67 acl read method GET
68 acl read method HEAD
69 # 寫參數:
70 acl write method PUT
71 acl write method POST
2. 在server2和server3中
在默認發佈目錄下放入index.php(選擇圖片的靜態頁面)和upload_file.php(上傳圖片的動態頁面),存放上傳圖片的目錄upload目錄。
cd /var/www/html
mkdir upload
chmod 777 upload
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中沒有