Forum Discussion
Sylvain_85827
Cirrus
Jul 02, 2013Rewrite URL based on selected node for load-balancing
Dears iRules Gurus and community,
Once again I need your help ^^ I'm wondering how to rewrite an URL of HTTP requests (IPP in fact) depending on the node chosen for load-balancing the traffic. ...
Sylvain_85827
Cirrus
Jul 09, 2013Hi,
it's me again 🙂 but this time only for giving you feedback.
@Kevin
I don't know why below code caught all the host name between https:// and the next / except the first letter!
if { [scan $uri_old {%*[http://|https://]%[^/]} host_old] } {
So I changed it like this:
if { [scan $uri_old {%[^//]//%[^/]]} host_tmp host_old] } { Certainly not the best, but at least it is working 😄
Finally the iRule became:
when RULE_INIT {
Log debug messages to /var/log/ltm? 1=yes, 0=no
set static::rewrite_debug 1
External hostname for the web application
set static::external "mypublicsite.com"
}
when HTTP_REQUEST_SEND {
Need to force the host header replacement and HTTP:: commands into the clientside context
as the HTTP_REQUEST_SEND event is in the serverside context
clientside {
Different action depending on the node selected for load-balancing
switch [LB::server addr] {
"1.1.1.1" {
HTTP::header replace Host "internalhost01.com"
if {$static::rewrite_debug}{log local0. "[IP::client_addr]:[TCP::client_port]: HTTP Host header rewritten with internalhost01.com"}
}
"1.1.1.2" {
HTTP::header replace Host "internalhost02.com"
if {$static::rewrite_debug}{log local0. "[IP::client_addr]:[TCP::client_port]: HTTP Host header rewritten with internalhost02.com"}
}
}
}
}
when HTTP_RESPONSE {
Replace all internal Host headers with $static::external value defined in RULE_INIT section
HTTP::header replace Host $static::external
if {$static::rewrite_debug}{log local0. "[IP::client_addr]:[TCP::client_port]: HTTP Host header rewritten with -> $static::external"}
if { [HTTP::is_redirect] } {
Log if we detect an HTTP redirection
if {$static::rewrite_debug}{log local0. "[IP::client_addr]:[TCP::client_port]: HTTP redirection detected"}
Store Location HTTP header coming from backend server
set uri_old [string tolower [HTTP::header Location]]
Store backend server host name
if { [scan $uri_old {%[^//]//%[^/]]} host_tmp host_old] } {
Replace backend server host name with external host name into HTTP Location header
set map_exp "set uri_new \[string map {$host_old $static::external} $uri_old\]"
eval $map_exp
Finally rewrite HTTP Location header
HTTP::header replace Location $uri_new
if {$static::rewrite_debug}{log local0. "[IP::client_addr]:[TCP::client_port]: HTTP Location header rewritten with -> $uri_new"}
}
}
}
I sincerely thank you all for your very valuable help! Without you this story would have taken me too much time, really thank you.
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)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