实验环境:rhel7.5
主机信息 | 作用 |
---|---|
server1(172.25.8.1) | haproxy |
server2(172.25.8.2) | 读的功能 |
server3(172.25.8.3) | 写的功能 |
真机 | 测试 |
当某主机被添加到黑名单里面的时候,报错让他转移到其他的页面连接
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg
67 frontend main *:80
68 acl blacklist src 172.25.8.250
69 http-request deny if blacklist
70 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
无法访问!!!
nginx:擅长处理静态页面
apache:擅长处理动态页面访问.php
#修改配置文件
在server3安装php,访问动态.php
默认访问的是server2,静态页面
访问.php文件的时候,访问的动态文件;
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg
68 acl blacklist src 172.25.8.111
69 http-request deny if blacklist
70 errorloc 403 http://172.25.8.1:8080/index.html
75 use_backend dynamic if { path_end .php } #php文件
76 default_backend static
81 backend static
82 balance roundrobin
83 server web1 172.25.8.2:80 check inter 1000
84 backend dynamic #访问php是访问的server3动态页面
85 balance roundrobin
86 server web2 172.25.8.3:80 check inter 1000
[root@server1 ~]# systemctl restart haproxy.service
[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
PUT/POST 上传
GET/HEAD 获取
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg
67 frontend main *:80
68 acl blacklist src 172.25.8.111
69 acl read method GET
70 acl write method POST
71 acl write method PUT
72 http-request deny if blacklist
73 errorloc 403 http://172.25.8.1:8080/index.html
78 use_backend dynamic if write
79 use_backend static if read
80 default_backend static
81
85 backend static
86 balance roundrobin
87 server web1 172.25.8.2:80 check inter 1000
88 backend dynamic
89 balance roundrobin
90 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
>
>
>
>
>
[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"] . "
>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "
>";
echo "Type: " . $_FILES["file"]["type"] . "
>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
>";
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";
}
?>
[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
>
>
>
>
>
[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"] . "
>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "
>";
echo "Type: " . $_FILES["file"]["type"] . "
>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb
>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
>";
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
#浏览器上测试: