运维企业实战——haproxy+apache实现页面的动静分离、读写分离

日志和黑名单

1.日志

vim /etc/rsyslog.conf
/etc/init.d/rsyslog restart

在这里插入图片描述
在这里插入图片描述
2.黑名单

 vim haproxy.cfg 
     
            acl blacklist src 172.25.60.250
            http-request  deny  if  blacklist
     
 /etc/init.d/haproxy restart

在这里插入图片描述
server1上安装httod,并把端口改为8080:

yum install httpd -y
vim /var/www/html/index.html
vim /etc/httpd/conf/httpd.conf 
	 136 Listen 8080
 
/etc/init.d/httpd start

vim haproxy.cfg
	 48         errorloc  403  http://172.25.60.1:8080/index.html
/etc/init.d/haproxy restart

在这里插入图片描述
在这里插入图片描述
测试:打开firefox:http://172.25.60.1/admin/stats

在黑名单里的主机访问会自动跳转到http://172.25.60.1:8080/index.html.

在这里插入图片描述
五、动静分离

访问静态的访问的是server2的默认发布页,访问以.php结尾的时,访问的是php动态页面。

1.编辑配置文件

vim haproxy.cfg 
        use_backend     static if { path_end .php }
        default_backend static
 
# The static backend backend for 'Host: img', /img and /css.
backend static
        balance         roundrobin
        server          web1    172.25.60.2:80 check inter 1000
 
# the application servers go here
backend dynamic
        balance         roundrobin
        server          web2    172.25.60.3:80 check inter 1000
 
 
/etc/init.d/haproxy restart

在这里插入图片描述
2.server3上下载php

yum install php -y
vim /var/www/html/index.php
<?php
         phpinfo();
?>
 
/etc/init.d/httpd restart

在这里插入图片描述
3.测试:

在真机上:curl 172.25.60.1

打开firefox:http://172.25.60.1/index.php

在这里插入图片描述
在这里插入图片描述
六、读写分离

1.编辑配置文件

 vim haproxy.cfg 
 
        acl write method POST
        acl write method PUT
 
 
        use_backend     dynamic if write
 
        default_backend static
 
# The static backend backend for 'Host: img', /img and /css.
backend static
        balance         roundrobin
        server          web1    172.25.60.2:80 check inter 1000
 
# the application servers go here
backend dynamic
        balance         roundrobin
        server          web2    172.25.60.3:80 check inter 1000
 
 
/etc/init.d/haproxy restart

在这里插入图片描述
2.在server2上上传两个php文件:

cd /var/www/html/
ls
chmod 777 index.php upload_file.php 
vim upload_file.php 
 
($_FILES["file"]["size"] < 2000000)) #扩大文件接收的数据
 
/etc/init.d/httpd restart

在这里插入图片描述
3.测试:

打开firefox:http://172.25.60.1/index.php
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44321029/article/details/89577629