Linux企业实战-Haproxy实现动静分离

朱雀 2023-07-13 04:13 200阅读 0赞

1.Haproxy实现动态界面与静态界面分离

注意:此实验是在上一篇博客的基础上做的
(1)配置haproxy服务器
在server1:

  1. [root@server1 html]# vim /etc/haproxy/haproxy.cfg
  2. 屏蔽掉上一篇博客添加的内容(配置文件尾部内容)
  3. 修改相关内容如下:
  4. 62 #---------------------------------------------------------------------
  5. 63 frontend westos *:80
  6. 64 acl url_static path_beg -i /images /javascript /stylesheets
  7. 65 acl url_static path_end -i .jpg .png .gif
  8. 66
  9. 67 use_backend static if url_static
  10. 68 default_backend app
  11. 69
  12. 70 #---------------------------------------------------------------------
  13. 71 # static backend for serving up images, stylesheets and such
  14. 72 #---------------------------------------------------------------------
  15. 73 backend static
  16. 74 balance roundrobin
  17. 75 server static 172.25.1.3:80 check
  18. 76
  19. 77 #---------------------------------------------------------------------
  20. 78 # round robin balancing between the various backends
  21. 79 #---------------------------------------------------------------------
  22. 80 backend app
  23. 81 #balance roundrobin
  24. 82 server web1 172.25.1.2:80 check
  25. 83 server web2 172.25.1.1:8000 check #backup
  26. 84 # server app3 127.0.0.1:5003 check

(2)配置动态页面服务器server1
下载安装apache,编辑测试页面并修改端口:

  1. [root@server1 ~]# yum install httpd -y
  2. [root@server1 ~]# cd /var/www/html/
  3. [root@server1 html]# vim index.html
  4. [root@server1 html]# cat index.html
  5. server1

由于server1同样也是haproxy代理服务器,为了防止端口冲突,将server1的htppd端口改为8000

  1. [root@server1 html]# vim /etc/httpd/conf/httpd.conf
  2. 修改端口号为8000
  3. 41 #Listen 12.34.56.78:80
  4. 42 Listen 8000
  5. [root@server1 html]# systemctl start httpd.service
  6. [root@server1 html]# systemctl restart haproxy.service

到此实现server1与server2的动态界面
在这里插入图片描述
在这里插入图片描述

(3)将server1设为backup状态

即server2服务器处于正常状态则只能访问到server2,若server2出现故障,server1顶上

  1. [root@server1 html]# vim /etc/haproxy/haproxy.cfg
  2. 修改如下
  3. 80 backend app
  4. 81 #balance roundrobin
  5. 82 server web1 172.25.1.2:80 check
  6. 83 server web2 172.25.1.1:8000 check backup
  7. [root@server1 html]# systemctl restart haproxy.service

测试访问172.25.1.1,则刷新页面内容不变,一直是server2
在这里插入图片描述

  1. [root@server2 ~]# systemctl stop httpd.service

再次刷新变为server1
在这里插入图片描述

(4) 配置静态页面服务器server3
创建images目录并存储一张图片

  1. [root@server3 ~]# cd /var/www/html/
  2. [root@server3 html]# pwd
  3. /var/www/html
  4. [root@server3 html]# mkdir images
  5. [root@server3 images]# ls
  6. 1.png
  7. [root@server3 ~]# systemctl start httpd

测试静态页面:浏览器输入172.25.1.1/images可以看到server3服务器apache下的访问目录

在这里插入图片描述
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,200人围观)

还没有评论,来说两句吧...

相关阅读