HAProxy的高級配置選項-ACL篇之域名重定向案例
作者:尹正傑
版權聲明:原創作品,謝絕轉載!否則將追究法律責任。
一.安裝Apache Httpd及准備測試數據
1>.試驗架構說明
node102.yinzhengjie.org.cn:
Haproxy服務器
node105.yinzhengjie.org.cn:
測試服務器,模擬客戶端
node106.yinzhengjie.org.cn:
Apache httpd服務器
node107.yinzhengjie.org.cn:
Apache httpd服務器
node108.yinzhengjie.org.cn:
Apache httpd服務器
2>.安裝Apache httpd服務
此過程相對簡單,我這里就直接略過了,可參考我之前的筆記:https://www.cnblogs.com/yinzhengjie/p/12114195.html
二.域名重定向實戰案例
1>.編輯haproxy的配置文件
[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg global maxconn 100000 chroot /yinzhengjie/softwares/haproxy stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin user haproxy group haproxy daemon nbproc 2 cpu-map 1 0 cpu-map 2 1 nbthread 2 pidfile /yinzhengjie/softwares/haproxy/haproxy.pid log 127.0.0.1 local5 info defaults option http-keep-alive option forwardfor option redispatch option abortonclose maxconn 100000 mode http timeout connect 300000ms timeout client 300000ms timeout server 300000ms listen status_page bind 172.30.1.102:8888 stats enable stats uri /haproxy-status stats auth admin:yinzhengjie stats realm "Welcome to the haproxy load balancer status page of YinZhengjie" stats hide-version stats admin if TRUE stats refresh 5s frontend WEB_PORT_80 bind 172.30.1.102:80 mode http #定義ACL,匹配客戶端請求的主機名 acl my_pc_page hdr_dom(host) -i pc.yinzhengjie.org.cn #定義ACL,匹配客戶端瀏覽器的類型 acl firefox_agent hdr(User-Agent) -m sub -i "Firefox" #調用ACL,如果客戶端請求的主機名是"pc.yinzhengjie.org.cn"則請求的資源會被重定向到博客園,和nginx的rewrite功能很像。 redirect prefix https://www.cnblogs.com/yinzhengjie/ if my_pc_page #調用ACL,如果使用火狐瀏覽器,則調度到firefox_web進行處理 use_backend firefox_web if firefox_agent #如果前面的ACL都沒有匹配成功就訪問默認的ACL default_backend backup_web backend firefox_web server web01 172.30.1.106:80 check inter 3000 fall 3 rise 5 server web02 172.30.1.107:80 check inter 3000 fall 3 rise 5 backend backup_web server web03 172.30.1.108:80 check inter 3000 fall 3 rise 5 [root@node102.yinzhengjie.org.cn ~]# [root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy [root@node102.yinzhengjie.org.cn ~]#
2>.編輯window客戶端的本地文件解析記錄,如下圖所示。
3>.任意瀏覽器訪問"http://pc.yinzhengjie.org.cn/"
4>.使用火狐瀏覽器訪問"http://node102.yinzhengjie.org.cn/"
5>.使用IE瀏覽器訪問"http://node102.yinzhengjie.org.cn/"
6>.使用curl模擬各種瀏覽器訪問"http://node102.yinzhengjie.org.cn/"