简单irules编写 入门级

可能有错,还烦请指出😸
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编程宝典》

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天学了点儿啥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值