使用Haproxy实现错误重定向,301,302重定向

雨点打透心脏的1/2处 2023-07-12 11:51 22阅读 0赞

在前面实验(https://blog.csdn.net/qq_35887546/article/details/104733906)的基础上实现错误重定向:

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

1.配置haproxy服务器

在server1:

  1. [root@server1 html]# vim /etc/haproxy/haproxy.cfg
  2. 63 frontend westos *:80
  3. 64 acl url_static path_beg -i /images
  4. 65 acl url_static path_end -i .jpg .gif .png
  5. 66 acl badhost src 172.25.63.250 #将客户端列为badhost
  6. 67 block if badhost #如果访问的客户端在badhoost里就阻挡
  7. 68 errorloc 403 http://172.25.63.1:8000 #如果服务器403(即服务器禁止访问)就将其重定向到http://172.25.63.1:8000
  8. 69 use_backend static if url_static
  9. 70 default_backend app
  10. 85 # server web2 172.25.63.1:8000 check #将之前有关实验内容注释掉

重启服务:

  1. [root@server1 html]# systemctl restart haproxy

2.测试

在客户端浏览器访问:172.25.63.1
在这里插入图片描述

3.实现301重定向

配置haproxy服务器:

  1. [root@server1 html]# vim /etc/haproxy/haproxy.cfg
  2. 63 frontend westos *:80
  3. 64 acl url_static path_beg -i /images
  4. 65 acl url_static path_end -i .jpg .gif .png
  5. 66 acl westos.org hdr_beg(host) -i westos.org
  6. 67 acl 172.25.63.1 hdr_beg(host) -i 172.25.63.1
  7. 68 #errorloc 403 http://172.25.63.1:8000
  8. 69 use_backend static if url_static
  9. 70 default_backend app
  10. 71 redirect code 301 location http://www.westos.org if westos.org #以westos.org访问就自动重定向到www.we stos.org
  11. 72 redirect code 301 location http://www.westos.org if 172.25.63.1 #以172.25.63.1访问就自动重定向到www.w estos.org

之后重启服务:

  1. [root@server1 html]# systemctl restart haproxy

在客户端做解析:

  1. [root@foundation63 ~]# vim /etc/hosts
  2. 172.25.63.1 www.westos.org westos.org

测试:
在客户端浏览器输入 westos.org 测试:
在这里插入图片描述在客户端浏览器输入 172.25.63.1 测试:

在这里插入图片描述

发表评论

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

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

相关阅读