Linux企业实战--Haproxy实现错误重定向

错误重定向就是当客户端访问服务器遇到指定错误时将其重定向到指定地址

注意此实验是在这篇博客的基础上做的https://blog.csdn.net/qq_36417677/article/details/104757756

(1) 配置haproxy服务器

在server1:

[root@server1 html]# vim /etc/haproxy/haproxy.cfg 

 63 frontend  westos *:80
 64     acl url_static       path_beg       -i /images
 65     acl url_static       path_end       -i .jpg .gif .png
 66     acl badhost src 172.25.1.250				#将客户端列为badhost
 67     block if badhost							#如果访问的客户端在badhoost里就阻挡
 68     errorloc 403 http://172.25.63.1:8000		#如果服务器403(即服务器禁止访问)就将其重定向到http://172.25.63.1:8000
 69     use_backend static          if url_static
 70     default_backend             app

 85 #    server  web2 172.25.1.1:8000 check				#将之前有关实验内容注释掉

重启服务:

[root@server1 html]# systemctl restart haproxy

(2)测试
在客户端浏览器访问:172.25.1.1
在这里插入图片描述

实现301永久重定向

配置haproxy服务器:

[root@server1 html]# vim /etc/haproxy/haproxy.cfg

 63 frontend  westos *:80
 64     acl url_static       path_beg       -i /images
 65     acl url_static       path_end       -i .jpg .gif .png
 66     acl westos.org hdr_beg(host)        -i westos.org
 67     acl 172.25.1.1 hdr_beg(host)       -i 172.25.1.1
 68     #errorloc 403 http://172.25.1.1:8000
 69     use_backend static          if url_static
 70     default_backend             app
 71     redirect code 301 location http://www.westos.org if westos.org              #以westos.org访问就自动重定向到www.we    stos.org
 72     redirect code 301 location http://www.westos.org if 172.25.1.1             #以172.25.1.1访问就自动重定向到www.w    estos.org

之后重启服务:

[root@server1 html]# systemctl restart haproxy

在客户端做解析:

[root@foundation1 ~]# vim /etc/hosts
[root@foundation1 ~]# cat /etc/hosts
172.25.1.1  www.westos.org  westos.org 

测试:
在客户端浏览器输入 westos.org 测试:则网页自动定位到www.westos.org
在这里插入图片描述
在客户端浏览器输入 172.25.1.1 测试:也自动定位到www.westos.org
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值