Redirect to different CDN based on GeoLocation
I added a STREAM profile to the virtual server and associated the following iRule.
when HTTP_REQUEST {
STREAM::disable
if { ([matchclass [IP::client_addr] equals $::test_group]) or ( [whereis [IP::client_addr] country] equals "CN") } {
log local0. "User is in country [whereis [IP::client_addr] country]"
set CDN1 "example-s.cdn1.net"
set CDN2 "example-s.cdn2.net"
}
}
when HTTP_RESPONSE {
if {[HTTP::header Location] contains "$CDN1"}{
HTTP::header replace Location [string map "$CDN1 $CDN2" [HTTP::header Location]]
}
STREAM::expression "@$CDN1@$CDN2@"
log local0. "A user in country [whereis [IP::client_addr] country] has been redirected from CDN1 to CDN2"
STREAM::enable
}
when STREAM_MATCHED {
log local0. "HOORAY"
}
I am looking for feedback for anything I may have missed or suggestions to make it better.
Thanks