在前面實驗(https://blog.csdn.net/qq_35887546/article/details/104733906)的基礎上實現錯誤重定向:
錯誤重定向就是當客戶端訪問服務器遇到指定錯誤時將其重定向到指定地址
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.63.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.63.1:8000 check #將之前有關實驗內容註釋掉
重啓服務:
[root@server1 html]# systemctl restart haproxy
2.測試
在客戶端瀏覽器訪問:172.25.63.1
3.實現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.63.1 hdr_beg(host) -i 172.25.63.1
68 #errorloc 403 http://172.25.63.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.63.1 #以172.25.63.1訪問就自動重定向到www.w estos.org
之後重啓服務:
[root@server1 html]# systemctl restart haproxy
在客戶端做解析:
[root@foundation63 ~]# vim /etc/hosts
172.25.63.1 www.westos.org westos.org
測試:
在客戶端瀏覽器輸入 westos.org
測試:
在客戶端瀏覽器輸入 172.25.63.1
測試: