标签:after 请求 variables pex 汉化 sig not nta false
用Netscaler的Variable和Assignment来实现计数控制Variable变量和Assignment调度的手册:
https://docs.citrix.com/en-us/netscaler/11-1/appexpert/variables.html
在负载均衡做各种复杂策略的时候往往会碰到需要前后有连续性的状态计数,netscaler作为一个策略封装比较友好的ADC在提供完善策略安全性保障的同时还提供了比较开放的编程环境。在编程中很关键的一环就是变量的定义和使用,我们就变量这一话题来看netscaler能够提供怎样的功能。本文抛砖引玉为目的,提供一个思路供大家参考,具体应用场景还需要大家发散性思考。
定义变量
长整型: ------------------------------- 数组型:
add ns variable name_variable -type ulong -init 1 -expires 3600
add ns variable my_variable -type "map(text(20),ulong,500)" -init 15 -expires 600
调用变量进行计算
进行计算的方式有如下:add加法、set重设成某个值、sub减法、append附加、clear清除
add ns assignment name_assign_add -variable "$name_variable" -add 1
add ns assignment my_assign -variable "$my_variable[HTTP.REQ.HEADER(\"CJ_Cookie\")]" -add 1
计算好的变量Variable可以在Policy里当Action使用,或者在表达式中来做判断依据
调用的方法如下:$variable_name.valueExists.其他表达式(返回布尔值)或$variable_name.valueCount.其他表达式(返回数值)
$name.valueExists(key-expression). Returns true if there is a value in the map selected by the key-expression. Otherwise returns false. This function will update the expiration and LRU information if the map entry exists, but will not create a new map entry if the value does not exist.
$name.valueCount. Returns the number of values currently held by the variable. This is the number of entries in a map. For a singleton variable, this is 0 if the variable is uninitialized or 1 otherwise.
例如:
$name_variable.GE(10);
$my_variable.valueCount.EQ(10)
add rewrite action rw_action_variable_waring2 insert_after_all "HTTP.RES.BODY(59999).SET_TEXT_MODE(IGNORECASE)" "\"this is waring\"" -pattern "随波汉化版"
add rewrite policy rw_policy_variable_waring "$name_variable.GE(10)" rw_action_variable_waring2
实现效果:当变量大于10的时候执行插入body的动作
或着在action中定义插入一个cookie值,名字为CJ_Count: 值为变量的值
add rewrite action rw_action_variable insert_http_header Set-Cookie "\"CJ_Count:\"+$name_variable"
测试案例use case:
定义变量,做为计数用户访问或登录次数
add ns variable name_variable -type ulong -init 1 -expires 3600
add ns variable name_variable_dele -type ulong -init 1 -expires 3600
调用两个变量并计算:
name_assign_add 每次增加1 ------------------ name_assign_dele 每次把变量清除
add ns assignment name_assign_add -variable "$name_variable" -add 1
add ns assignment name_assign_dele -variable "$name_variable" -clear
Rewrite策略:
rewrite策略实现访问URL=03时,variable增加1,访问URL=callout时,variable清除;请求header中Accetp为text时插入COOKIE
add rewrite policy rw_policy_variable "HTTP.REQ.HEADER(\"Accept\").CONTAINS(\"text\")" rw_action_variable
add rewrite policy rw_policy_variable_add "HTTP.REQ.URL.PATH.GET(1).EQ(\"03\")" name_assign_add
add rewrite policy rw_policy_variable_dele "HTTP.REQ.URL.PATH.GET(1).EQ(\"callout\")" name_assign_dele
action为:插入cookie,名字为CJ_Count,值为变量的值
add rewrite action rw_action_variable insert_http_header Set-Cookie "\"CJ_Count:\"+$name_variable"
效果:
登录主页时,由于accept为text,遂插入cookie:CJ_Cookie=1
每次访问URL=/03时Cookie的值+1
访问URL=/callout时,把cookie的值清除
用Netscaler的Variable和Assignment来实现计数控制
标签:after 请求 variables pex 汉化 sig not nta false
原文地址:http://blog.51cto.com/caojin/2061350