HAProxy的ACL調度規則
HAProxy的ACL可以對接收到的報文進行匹配和過濾,基於請求報文頭部中的源地址、源端口、目標地址、目標端口、請求方法、URL、文件後綴等信息內容進行匹配並執行進一步操作。
acl的定義
語法格式:
acl <aclname> <criterion> [flags] [operator] [<value>]
acl acl名稱 條件 條件標記位 具體操作符 操作對象類型
criterion
hdr([<name> [,<occ>]]):完全匹配字符串
hdr_beg([<name> [,<occ>]]):前綴匹配
hdr_dir([<name> [,<occ>]]):路徑匹配
hdr_dom([<name> [,<occ>]]):域匹配
hdr_end([<name> [,<occ>]]):後綴匹配
hdr_len([<name> [,<occ>]]):長度匹配
hdr_reg([<name> [,<occ>]]):正則表達式匹配
hdr_sub([<name> [,<occ>]]):子串匹配
dst 目標IP
dst_port 目標PORT
src 源IP
src_port 源PORT
flags
-i 不區分大小寫
-m 使用指定的pattern匹配方法
-n 不做DNS解析
-u 禁止acl重名,否則多個同名ACL匹配或關
operator
整數比較:eq、ge、gt、le、lt
字符比較:
- exact match (-m str) :字符串必須完全匹配模式
- substring match (-m sub) :在提取的字符串中查找模式,如果其中任何一個被發現,ACL將匹配
- prefix match (-m beg) :在提取的字符串首部中查找模式,如果其中任何一個被發現,ACL將匹配
- suffix match (-m end) :將模式與提取字符串的尾部進行比較,如果其中任何一個匹配,則ACL進行 匹配
- subdir match (-m dir) :查看提取出來的用斜線分隔(“/”)的字符串,如果其中任何一個匹配,則 ACL進行匹配
- domain match (-m dom) :查找提取的用點(“.”)分隔字符串,如果其中任何一個匹配,則ACL進 行匹配
value
- Boolean #布爾值false,true
- integer or integer range #整數或整數範圍,比如用於匹配端口範圍,1024~32768 - IP address / network #IP地址或IP範圍, 192.168.0.1 ,192.168.0.1/24
-string exact –精確比較
substring—子串 www.magedu.com
suffix-後綴比較
prefix-前綴比較
subdir-路徑,/wp-includes/js/jquery/jquery.js
domain-域名,www.magedu.com
- regular expression #正則表達式
- hex block #16進制
acl的調用
多個acl作爲條件時的邏輯關係
與:隱式(默認)使用
或:使用"or" 或 "||"表示
否定:使用"!" 表示
基於acl定義不同的域名調度到不同的後端服務器
示例1:根據用戶的請求的域名的不同做匹配
1.修改haproxy配置文件
frontend web
bind 172.20.27.20:80
mode http
acl pc_web_page hdr(host) -i www.mylinuxops.com #忽略大小寫匹配報文頭部的主機名
acl m_web_page hdr(host) -i m.mylinuxops.com
use_backend pc_server if pc_web_page #ACL的調用
use_backend m_server if m_web_page
default_backend backup_server #當請求都不符合時調轉到默認頁面
backend pc_server
server web1 192.168.27.21:80 weight 1 check inter 3s fall 3 rise 5
backend m_server
server web2 192.168.27.22:80 weight 1 check inter 3s fall 3 rise 5
backend back_server
server web2 192.168.27.22:80 weight 1 check inter 3s fall 3 rise 5
2.測試
[root@localhost ~]# vim /etc/hosts
[root@localhost ~]# curl m.mylinuxops.com #匹配到規則後調用後端對應的服務器
m.mylinuxops.com
[root@localhost ~]# curl www.mylinuxops.com
www.mylinuxops.com
[root@localhost ~]# curl dl.mylinuxops.com #沒有匹配到規則調用默認的服務器
welcome to mylinuxops.com
acl的與、或和非
與:當用戶請求的域名即滿足第一個匹配條件也滿足第二個條件時就調用後端的服務器組
或:當用戶請求的域名滿足第一個匹配條件或者第二個條件就調用某個後端服務器組
非:當用戶的請求不滿足條件是調用某個後端的服務器組
示例:或的使用
1.修改haproxy配置文件
frontend web
bind 172.20.27.20:80
mode http
acl pc_web_page hdr_dom(host) -i www.mylinuxops.com #配置acl規則,規則名字pc_web_page,匹配用戶請求的域名,忽略大小寫,匹配的內容爲www.mylinuxops.com
acl mobile_web_page hdr_dom(host) -i m.mylinuxops.com #配置acl規則,規則名字mobile_web_page,匹配用戶請求的域名,忽略大小寫,匹配的內容爲m.mylinuxops.com
use_backend pc_server if pc_web_page #配置調用規則,當匹配規則所定義的條件時,調用pc_host所定義的後端服務器
use_backend mobile_server if mobile_web_page #配置調用規則,當匹配規則所定義的條件時,調用mobile_host所定義的後端服務器
backend pc_server
server web1 192.168.27.21:80 weight 1 check inter 3s fall 3 rise 5
backend mobile_host
server web2 192.168.27.22:80 weight 1 check inter 3s fall 3 rise 5
backend backup_server
server web2 192.168.27.22:80 weight 1 check inter 3s fall 3 rise 5
2.測試
[root@localhost ~]# curl www.mylinuxops.com
www.mylinuxops.com #返回的爲web1上虛擬主機定義的主頁面
[root@localhost ~]# curl m.mylinuxops.com
ojbk #由於後端服務器沒有此域名的虛擬主機,所以返回了一個服務器默認的index.html頁面
acl基於源地址做調度
使用acl,對IP地址做匹配,做到從某個地址或者地址段來的就調度到哪個後端的服務器上
示例:
frontend web
bind 172.20.27.20:80
mode http
acl ip_range_test src 192.168.27.0/24 172.20.27.10
use_backend m_server if ip_range_test
default_backend backup_server
backend pc_server
server web1 192.168.27.21:80 weight 1 check inter 3s fall 3 rise 5
backend m_server
server web2 192.168.27.22:80 weight 1 check inter 3s fall 3 rise 5
backend backup_server
server web2 192.168.27.22:80 weight 1 check inter 3s fall 3 rise 5
測試
查看nginx上測試頁面
[root@web2 ~]# cat /data/www/index.html
m.mylinuxops.com
[root@web2 ~]# cat /apps/nginx/html/index.html
welcome to mylinuxops.com
在172.20.27.10上測試訪問
[root@localhost ~]# curl m.mylinuxops.com
m.mylinuxops.com #訪問到了m.mylinuxops.com的主頁面
[root@localhost ~]# curl www.mylinuxops.com
welcome to mylinuxops.com #由於後端沒有設定www.mylinux.com的virtualserver所裏跳轉到了默認頁面
acl基於地址段做拒絕
使用acl對地址段做判斷,對匹配的做拒絕操作
frontend web
bind 172.20.27.20:80
mode http
acl ip_range_test src 192.168.27.0/24 172.20.27.10
block if ip_range_test #對ip地址段做拒絕時使用block
default_backend backup_server
#option httpchk HEAD /monitor-page/index.html HTTP/1.0
backend pc_server
server web1 192.168.27.21:80 weight 1 check inter 3s fall 3 rise 5
backend m_server
server web2 192.168.27.22:80 weight 1 check inter 3s fall 3 rise 5
backend backup_server
server web2 192.168.27.22:80 weight 1 check inter 3s fall 3 rise 5
測試
使用172.20.27.10主機進行訪問
[root@localhost ~]# curl www.mylinuxops.com
<html><body><h1>403 Forbidden</h1> #被拒絕
Request forbidden by administrative rules.
</body></html>
[root@localhost ~]# curl m.mylinuxops.com
<html><body><h1>403 Forbidden</h1>
Request forbidden by administrative rules.
</body></html>
acl基於用戶瀏覽器的類型
1.取得各瀏覽器的user-agent信息
#google
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36
#FireFox
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0
取出user-agent中具有特徵意義的部分作爲匹配條件,google(Chrome),FireFox(Firefox)
2.修改配置文件
frontend web
bind 172.20.27.20:80
mode http
acl google hdr(user-agent) -m sub -i "Chrome" #匹配請求首部user-agent中的字串是否包含chrome
acl firefox hdr(user-agent) -m sub -i "Firefox" #匹配字串中是否有Firefox
use_backend pc_server if google
use_backend m_server if firefox
default_backend backup_server
backend pc_server
server web1 192.168.27.21:80 weight 1 check inter 3s fall 3 rise 5
backend m_server
server web2 192.168.27.22:80 weight 1 check inter 3s fall 3 rise 5
backend backup_server
server web2 192.168.27.22:80 weight 1 check inter 3s fall 3 rise 5
測試
使用不同瀏覽器訪問時分別被送到後端不同的服務器上
acl規則匹配後重定向
當被acl規則匹配到後也可以將訪問的url進行重定向,使用redirect進行調用規則
示例:當瀏覽器爲firefox時調度到baidu.com
frontend web
bind 172.20.27.20:80
mode http
acl google hdr(user-agent) -m sub -i "Chrome"
use_backend pc_server if google
acl firefox hdr(user-agent) -m sub -i "Firefox"
redirect prefix http://www.baidu.com if firefox #當瀏覽器爲firefox時重定向到百度
backend pc_server
server web1 192.168.27.21:80 weight 1 check inter 3s fall 3 rise 5
backend m_server
server web2 192.168.27.22:80 weight 1 check inter 3s fall 3 rise 5
測試
基於HAProxy實現匹配後綴做動靜分離
haproxy可以使用acl中的path_end,基於所要訪問的文件的後綴做動靜分離的匹配
示例:
1.修改HAProxy配置文件
frontend web
bind 172.20.27.20:80
mode http
acl php path_end -i .php #配置acl acl名爲php 匹配後綴爲 .php 忽略大小寫
acl image path_end -i .jpg .jpeg .gif .png #配置acl acl名爲image 匹配後綴爲 .jpg .jpeg .gif .png 忽略大小寫
use_backend php_server if php #配置調用規則當匹配php的條件時,調用php_server
use_backend image_server if image #匹配調用規則當匹配image的條件時,調用image_server
backend php_server
server web1 192.168.27.21 check port 80 inter 3s fall 3 rise 5
backend image_server
server web1 192.168.27.22 check port 80 inter 3s fall 3 rise 5
2.修改nginx+php服務器配置
[root@php ~]# vim /apps/nginx/conf/servers/mylinuxops.conf
server {
server_name www.mylinuxops.com;
listen 80;
access_log /apps/nginx/logs/mylinuxops.log access_json;
location / {
root /data/www;
index index.html;
}
location ~ \.php$ {
root /data/php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
3.創建網站頁面
[root@php ~]# mkdir /data/php/
[root@php ~]# vim /data/php/index.php
<?php
phpinfo();
?>
4.修改靜態服務器配置文件
[root@image ~]# vim /apps/nginx/conf/servers/mylinuxops.conf
server {
server_name www.mylinuxops.com;
listen 80;
access_log /apps/nginx/logs/mylinuxops.log access_json;
location / {
root /data/www;
index index.html;
}
}
5.在資源路徑下添加圖片
[root@localhost ~]# ls /data/www/
1.png index.html
測試
基於路徑的頭部做動靜分離
haproxy中也可以基於文件訪問的頭部做匹配將其進行動靜分離,比如某幾個目錄內都爲靜態文件,則將這幾個目錄進行匹配
示例:
修改配置文件
frontend web
bind 172.20.27.20:80
mode http
acl php_path path_end -i php #定義匹配PHP後綴文件
acl static_path path_beg -i /static /images /javascript /tfs #定義匹配靜態文件的目錄
use_backend php_server if php_path #php後綴文件調用後端php_server
use_backend static_server if static_path #靜態資源路徑時調用後端static_server
backend php_server
server web1 192.168.27.21:80 weight 1 check inter 3s fall 3 rise 5
backend static_server
server web2 192.168.27.22:80 weight 1 check inter 3s fall 3 rise 5
backend backup_server
配置後端php服務器
[root@php ~]# cat /data/www/php/index.php
<?php
phpinfo();
?>
配置後端static服務器
[root@static ~]# ls /data/www/images/1.png
/data/www/images/1.png
測試訪問以php結尾文件
測試訪問/images開頭的文件
http基於策略的訪問控制
修改haproxy配置文件
frontend web
bind 172.20.27.20:80
mode http #http的訪問控制需要基於http的模式來進行
acl php_path path_end -i php
acl static_path path_beg -i /static /images /javascript /tfs
acl client_deny src 172.20.27.0/18 #定義拒絕訪問的源地址
http-request deny if client_deny #對匹配到的源地址發來的請求進行拒絕
use_backend php_server if php_path
use_backend static_server if static_path
backend php_server
server web1 192.168.27.21:80 weight 1 check inter 3s fall 3 rise 5
backend static_server
server web2 192.168.27.22:80 weight 1 check inter 3s fall 3 rise 5
backend backup_server
server web2 192.168.27.22:80 weight 1 check inter 3s fall 3 rise 5
使用172.20.27.0網段的客戶機測試訪問
[root@localhost ~]# curl www.mylinuxops.com
<html><body><h1>403 Forbidden</h1> #請求被拒絕
Request forbidden by administrative rules.
</body></html>