Haproxy实现负载均衡—指定日志存放目录&重定向
1.指定日志存放目录
修改日志服务配置文件从而修改日志存储位置:
vim /etc/rsyslog.conf
打开15 16行的注释,添加55行日志存储位置:
15 $ModLoad imudp
16 $UDPServerRun 514
55 local2.* /var/log/haproxy
systemctl restart rsyslog #重启日志服务
cat /var/log/haproxy
2.错误重定向
修改配置文件写入错误重定向策略:
cd /etc/haproxy
vim haproxy.cfg
把85行注释掉,并写入下面内容:
66 acl badhost src 172.25.254.36 #真机ip
67 block if badhost
68 errorloc 403 http://172.25.254.1:8000
systemctl restart haproxy #如果报错,检查server2和server3的http是否启动
测试:
访问172.25.254.1---被重定向到172.25.254.1:8000---看到server1
3.自动重定向
修改配置文件写入自动重定向策略:
cd /etc/haproxy
vim haproxy.cfg
66 acl westos.org hdr_beg(host) -i westos.org
67 acl 172.25.254.1 hdr_beg(host) -i 172.25.254.1
70 redirect code 301 location http://www.westos.org if westos.org
71 # 以westos.org访问 自动重定向到 www.westos.org
72 redirect code 301 location http://www.westos.org if 172.25.254.1
73 # 以172.25.254.1访问 自动重定向到 www.westos.org
systemctl restart haproxy
在真机中添加解析:
vim /etc/hosts
172.25.254.1 www.westos.org westos.org
测试:
访问westos.org---重定向到---www.westos.org
访问172.25.254.1---重定向到---www.westos.org