Forum Discussion
santosh_81454
Sep 05, 2012Nimbostratus
Redirect every 4th reuqest to external site
We are in process of migrating our old website to a new website. The new website is hosted on a different set of servers. As per management decision at the initial phase we are asked to redirect 25% ...
Kevin_Stewart
Sep 05, 2012Employee
I think the problem with this implementation is that CLIENT_ACCEPTED fires several times within a user session. So while some of the connections won't initially be redirected, they eventual will trigger the redirect logic and be sent to the other server.
Given that this is HTTP traffic, you probably also want to be mindful of sessions. Here's a crude little example of sending every fourth *NEW* request to the new site by way of a tracking cookie and counter session table.
when HTTP_REQUEST {
if the tracking cookie doesn't exist, determine skip count and redirect if necessary
if { not ( [HTTP::cookie exists TRACKER] ) } {
log local0. "TRACKER cookie doesn't exist"
set cookie flag
set setcookie 1
detemine skip count (%25 of traffic)
if { [table lookup -subtable TRACKERTABLE "track"] eq "" } {
log local0. "tracker table doesn't exist - create it and set value to 1"
table set -subtable TRACKERTABLE "track" 1
} elseif { [expr [table lookup -subtable TRACKERTABLE "track"] < 4] } {
log local0. "tracker table value less than 4 - increment and allow local traffic"
table incr -subtable TRACKERTABLE "track"
} elseif { [table lookup -subtable TRACKERTABLE "track"] == 4 } {
log local0. "tracker table equals 4 - reset to 1 and redirect to new site"
table set -subtable TRACKERTABLE "track" 1
HTTP::redirect "https://www.xyz.com"
}
} else {
log local0. "TRACKER cookie exists"
}
}
when HTTP_RESPONSE {
if setcookie exists, create TRACKER cookie in response
if { [info exists setcookie] and $setcookie == 1 } {
HTTP::cookie insert name TRACKER value 1
unset setcookie
}
}
So when the user connects for the first time, they won't have the tracker cookie. You consult the session table to determine the count, redirect at 4 (and reset to 1), and simply increment the value if not 4. If the connection is not redirected, the HTTP_RESPONSE sends the tracker cookie so that all future requests from this client will not get redirected.
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects