可能有错,还烦请指出😸
irules作用:系统对数据控制有更精细的定制要求;对标准协议的功能支持有更高的要求
irules使用TCL语句:
#:注释,以;或换行结束
[命令](命令嵌套时使用[])
::全局变量声明符
class:全局数组变量,四种类型:address(IP地址)、string(字符串)、integer(数值)、external fail(外部文件)
;
matchclass:匹配语句,参数2为匹配条件,1和3为匹配参数
置换符:$
(例
set a 100
set b $a
将a的值给b
),设置或改变变量值时不需要:$
双引号中:{}中字符按照普通字符处理
(例:
set b {$a + 2}
expr $b*4
得出结果为a+2*4
若想要输出结果为(a+2)*4
set b {[expr$a+2]}
expr $b*4
)
操作符
Irules中包含事件:
When HTTP_REQUEST{}
When HTTP_RESPONSE{}
[HTTP::uri]:HTTP代表HTTP协议,uri表示取这个访问中的uri,::表示从属关系,uri属于 HTTP命令体系
常用逻辑体系:
if{}{
}elseif{
}
else{
}
Switch(options)<>{
Case string:
<>
Break;
Default:
<>
Break;
}
输出log:log[<facility>.<level>]<message>
#注意空格缩进
一些简单Irules:
1、删除response Header头内容
when HTTP_RESPONSE {
log local0. "Removing unwanted headers - $RequestedURL"
HTTP::header remove "Server"
HTTP::header remove "X-Powered-By"
HTTP::header remove "X-Pad"
}
2.1、负载:根据不同地址分配到不同pool
when HTTP_REQUEST {
if { [IP::addr [IP::remote_addr] equals 10.10.X.0/24] } {
Log local0. "Load balancing to in_network_http_pool"
pool /Common/in_network_http_pool
} else {
log local0. "Load balancing to out_network_http_pool"
pool /Common/out_network_http_pool
}
}
2.2、根据不同uri负载到不同的pool
When HTTP_REQUEST{
If{ [HTTP::uri] contains “数据” }{
Pool 指定pool
}else{
Pool all_pool}
}
}
3、插入X-Forward-for
when HTTP_RESPONSE {
HTTP::header insert X-Forwarded-For [IP::remote_addr]]
}
4、修改页面返还内容
when HTTP_RESPONSE {
if { [HTTP::status] eq "404" } {
log local0. "HTTP 404 on $RequestedURL"
HTTP::respond 200 content "<html>
<head><title>Apology Page</title></head>
<body>Oops,I got lost(修改后展现的页面)</body>
</html>"
event disable
}
}
5、禁用事件
event disable
6、添加优先级
when HTTP_RESPONSE priority 100 {
7、重定向
When HTTP_REQUEST{
If{ [HTTP::uri] contains “关键字”}{
HTTP::redirect”https://[HTTP::host][HTTP::uri]”
}
}
8、包含关键字访问a,否则访问b:
When HTTP_REQUEST{
If{ [HTTP::uri] contains “关键字”}{
HTTP::redirect”https://a.com”
}else{
HTTP::redirect”https://b.com}
}
}
9.1、会话保持 基于header头
例:
header包头
When HTTP_REQUEST{
If{ [HTTP::header “header头内字段”] !=”” }{
Persist uie [HTTP::header “header头内字段”]
}
}
9.2、基于cookie(session ID)会话保持
10、示例
when HTTP_REQUEST {
if { [IP::addr [IP::remote_addr] equals 10.10.4.0/24] } {
log local0. "Load balancing to in_network_http_pool"
pool /Common/in_network_http_pool
} else {
log local0. "Load balancing to out_network_http_pool"
pool /Common/out_network_http_pool
}
log local0. "Inserting XFF header for [IP::remote_addr]"
HTTP::header insert X-Forwarded-For [IP::remote_addr]
set RequestedURL [HTTP::host][HTTP::uri]
}
when HTTP_RESPONSE {
if { [HTTP::status] eq "404" } {
log local0. "HTTP 404 on $RequestedURL"
HTTP::respond 200 content "<html>
<head><title>Apology Page</title></head>
<body>Oops,I got lost</body>
</html>"
event disable
}
}
参考:《Irules编程宝典》