搭建环境
实验环境:rhel7.5
主机信息 | 作用 |
---|---|
server1(172.25.8.1) | haproxy |
server2(172.25.8.2) | 读的功能 |
server3(172.25.8.3) | 写的功能 |
真机 | 测试 |
#1 Haproxy设置黑名单
当某主机被添加到黑名单里面的时候,报错让他转移到其他的页面连接
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg 67 frontend main *:8068 acl blacklist src 172.25.8.25069 http-request deny if blacklist70 errorloc 403 http://172.25.8.1:8080/index.html
[root@server1 ~]# yum install -y httpd
[root@server1 ~]# vim /var/www/html/index.html
无法访问!!!
#本机上安装http,只是为了提供8080端口
[root@server1 ~]# systemctl start httpd
[root@server1 ~]# netstat -anltp
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf 42 Listen 8080
[root@server1 ~]# systemctl restart haproxy.service
#浏览器测试:
输入:172.25.8.1:8080/index.html
[kiosk@foundation8 ~]$ curl 172.25.8.1:8080
无法访问!!!
#2 Haproxy动静分离
nginx:擅长处理静态页面
apache:擅长处理动态页面访问.php
#修改配置文件
在server3安装php,访问动态.php
默认访问的是server2,静态页面
访问.php文件的时候,访问的动态文件;
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg 68 acl blacklist src 172.25.8.11169 http-request deny if blacklist70 errorloc 403 http://172.25.8.1:8080/index.html75 use_backend dynamic if {
path_end .php } #php文件76 default_backend static81 backend static82 balance roundrobin83 server web1 172.25.8.2:80 check inter 100084 backend dynamic #访问php是访问的server3动态页面85 balance roundrobin86 server web2 172.25.8.3:80 check inter 1000
[root@server1 ~]# systemctl restart haproxy.service
#在server3上安装php
[root@server3 ~]# yum install -y php
[root@server3 ~]# vim /var/www/html/index.php
<?php
phpinfo()
?>
[root@server3 ~]# systemctl restart httpd.service
#在server2上也安装php
[root@server2 ~]# yum install -y php
[root@server2 ~]# vim /var/www/html/index.php
<?php
phpinfo()
?>
[root@server2 html]# vim /var/www/html/index.html
server2
[root@server2 ~]# systemctl restart httpd.service
在主机上测试:
[root@foundation8 ~]# curl 172.25.8.1
server2
[root@foundation8 ~]# curl 172.25.8.1/index.php #server3的动态页面
#浏览器测试
输入:172.25.8.1
输入:172.25.8.1/index.php
#3 Haproxy读写分离(server2读;server3写)
PUT/POST 上传
GET/HEAD 获取
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg 67 frontend main *:8068 acl blacklist src 172.25.8.11169 acl read method GET70 acl write method POST71 acl write method PUT72 http-request deny if blacklist73 errorloc 403 http://172.25.8.1:8080/index.html78 use_backend dynamic if write79 use_backend static if read80 default_backend static81 85 backend static86 balance roundrobin87 server web1 172.25.8.2:80 check inter 100088 backend dynamic89 balance roundrobin90 server web2 172.25.8.3:80 check inter 1000
[root@server1 ~]# systemctl restart haproxy.service
#在server2上
[root@server2 html]# ls
index.html index.php upload upload_file.php
[root@server2 html]# chmod 755 index.php
[root@server2 html]# ll
[root@server2 html]# vim index.php
<html>
<body><form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename(server2):</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form></body>
</html>
[root@server2 html]# vim 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";}
?>
#在server3上
[root@server3 html]# ls
index.html index.php upload
[root@server3 html]# chmod 777 upload/
[root@server3 html]# mv upload/* .
[root@server3 html]# scp -r *.php upload server2:/var/www/html/[root@server3 html]# vim index.php
<html>
<body><form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename(server3):</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form></body>
</html>[root@server3 upload]# vim upload_file.php
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000000)){
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";}
?>
![在这里插入图片描述](https://img-blog.csdnimg.cn/2019110503
#浏览器上测试:
#查看是否上传成功
#server3上写入成功;
#server2上无写入
#读写分离成功