HAproxy安装配置重定向及读写分离
server1:172.25.88.1
server3:172.25.88.3
server4:172.25.88.4
简单配置
server1:
yum install haproxy -y
vim /etc/haproxy/haproxy.cfg
60 #---------------------------------------------------------------------
61 # main frontend which proxys to the backends
62 #---------------------------------------------------------------------
63 #frontend main *:5000
64 # acl url_static path_beg -i /static /images /javascript /stylesheets
65 # acl url_static path_end -i .jpg .gif .png .css .js
66 #
67 # use_backend static if url_static
68 # default_backend app
69 #
70 #---------------------------------------------------------------------
71 # static backend for serving up images, stylesheets and such
72 #---------------------------------------------------------------------
73 #backend static
74 # balance roundrobin
75 # server static 127.0.0.1:4331 check
76 #
77 #---------------------------------------------------------------------
78 # round robin balancing between the various backends
79 #---------------------------------------------------------------------
listen westos *:80
balance roundrobin
server web1 172.25.88.3:80 check
server web2 172.25.88.4:80 check
89 listen admin *:8080 haproxy 监控页面:用浏览器访问172.25.88.1:8080/status
90 stats enable
91 stats uri /status 监控页面地址
92 stats auth admin:westos 用户名:密码
93 stats refresh 5s
server3:
yum install httpd -y
server4:
yum install httpd -y
检验:
浏览器访问172.25.88.1 rr:3/4
浏览器访问172.25.88.1:8080/status
HAproxy日志
日志默认存入rsyslog,要查看的话,需要打开rsyslog
server1:
vim /etc/rsyslog.conf
13 $ModLoad imudp 接受 haproxy 日志
14 $UDPServerRun 514
42 *.info;mail.none;authpriv.none;cron.none;local2.none /var/log/messages
62 local2.* /var/log/haproxy.log 单独存一份
/etc/init.d/rsyslog start
tail -f /var/log/haproxy.log
server1:
vim /etc/haproxy
86 frontend westos *:80
87 acl url_static path_beg -i /images
88 acl url_static path_end -i .jpg .gif .png
89 use_backend static if url_static
90 default_backend app
91
92 backend static 后端静态
93 balance roundrobin
94 server web1 172.25.88.3:80 check
95
96 backend app
97 balance roundrobin
98 server web2 172.25.88.4:80 checkserver3:
/etc/init.d/httpd start
ll /var/www/html/images/ OSI.gif RedHat.jpg
访问的是server3,因为只有server3才有images目录 server4:
/etc/init.d/httpd start
重定向
server1:
先设置黑名单
vim /etc/haproxy
90 acl badhost src 172.25.88.250
91 block if badhost直接给用户返回403,不太友好,所以重定向应运而生
错误重定向(403)
vim /etc/httpd/conf/httpd.conf Listen 8000
vim /var/www/html/index.html 攻城师正在修复ING... 其实我就是不想让你访问!!!啦啦啦!
server1:
vim /etc/haproxy