Forum Discussion
Christian_15126
Jan 07, 2013Nimbostratus
Removing the * fixed it! I don't know why I didn't think of that before.. starts_with is the same as \?* anyways.. Thanks for your help! On a side note (but this isn't a deal breaker at all), how would I get this working using a switch statement instead of an if? I tried using a switch statement to clean up the code, but I couldn't get it to work (see below). No worries at all if it's too much work to figure out, I'm just trying to get away from if statements and move to switch to reduce code. I tried the following, but the switch statements don't seem to match correctly.. Thanks for your help!
Match subdomain.domain, with a path "/", b_path, c_path; Then A/B load balance 60/40 percent ratio.
Created 12/11/2012 Christian Henderson
Last edited 12/11/12
when RULE_INIT {
log local0. "initializing ... "
set static::debug 1
Host header variables for sub-domain and domain.
set static::a_sub "degrees"
set static::b_sub "onlinedegrees"
set static::c_sub "info"
set static::domain "blah.edu"
Path variables.
set static::b_path "\?"
set static::c_path "index.htm"
set static::d_path "degrees"
Current count variable.
set static::current 0
1 in N requests will go to B subdomain - 6 for 60/40 ratio.
set static::ratio 6
Max value to reset count.
set static::max 10
}
when HTTP_REQUEST {
F5 doesn't put the question mark in the query string
if { [HTTP::query] equals "" } { set new_query "" }
else { set new_query "?[HTTP::query]" }
if { $static::debug > 1 } { log local0. "new query string is \[$new_query\]" }
Only catch requests that come in with the proper host header, and matching /, b_path,c_path, or d_path.
if { [string tolower [HTTP::host]] equals "$static::c_sub.$static::domain" } {
Only catch clients that come in through the URI path static::a_path, static::b_path, or static::c_path
switch -glob [string tolower [HTTP::uri]] "/" -
"/$static::b_path*" -
"/$static::c_path*" -
"/$static::d_path*" {
Check for < current for 60% of traffic.
if { $static::current < $static::ratio } {
Redirect traffic to A subdomain
if { $static::debug } { log local0. "redirect FROM [HTTP::uri] TO http://${static::a_sub}.${static::domain}/$new_query" }
HTTP::respond 301 Location "http://${static::a_sub}.${static::domain}/$new_query" "Cache-Control" "no-cache, must-revalidate"
Increment the counter
incr static::current
log local0. "A count is ${static::current} with ratio ${static::ratio}"
} else {
Redirect traffic to B subdomain for 40% requests
if { $static::current < $static::max } {
Redirect traffic to B subdomain
if { $static::debug } { log local0. "redirect FROM [HTTP::uri] TO http://${static::b_sub}.${static::domain}/$new_query" }
HTTP::respond 301 Location "http://${static::b_sub}.${static::domain}/$new_query" "Cache-Control" "no-cache, must-revalidate"
if { $static::debug > 1 } { log local0. "round robin is $static::round_robin" }
Increment the counter
incr static::current
log local0. "B count is ${static::current} with max ${static::max}"
} else {
If current count is 10, reset count to 0 and redirect traffic to A subdomain
if { $static::current >= $static::max } {
set static::current 0
log local0. "Reset current variable to ${static::current} "
if { $static::debug } { log local0. "redirect FROM [HTTP::uri] TO http://${static::a_sub}.${static::domain}/$new_query" }
HTTP::respond 301 Location "http://${static::a_sub}.${static::domain}/$new_query" "Cache-Control" "no-cache, must-revalidate"
TCP::close
event HTTP_REQUEST disable
}
}
}
}
}
}