Forum Discussion
John_LeMay_1062
Nimbostratus
Apr 08, 2008Improving on this "catch all"?
Recently I configured a "catch all" type virtual server and I'm handling various aspects of traffic management with an iRule. So far it's working well, but it is already becoming difficult to manage. The rule follows and should be, I believe, relatively self-explanatory except for the "matchclass" statement. That statement is used to differentiate between internal and external clients, redirecting the external clients to a separate pool of servers.
I was hoping for some tips on improving this concept to make it easier on the eyes and simpler to manage going forward. I'm also concerned about performance as this rule continues to grow as we utilize it for additional sites. Host names and other info have been made more generic for the sake of posting here.
when HTTP_REQUEST {
if { [HTTP::host] equals "site1.domain.com" }{
pool pool_site1.domain.com
} elseif { [HTTP::host] equals "site2.domain.com" }{
if { [HTTP::uri] eq "/" }{
HTTP::uri "/pathtosite/site2"
}
if { [matchclass [IP::client_addr]/16 equals $::mySubnets] }{
pool pool_site2.domain.com_internal
} else {
pool pool_site2.domain.com
}
} elseif { [HTTP::host] equals "site3.domain.com" }{
pool pool_site3.domain.com
} else {
reject
}
}
- hoolio
Cirrostratus
The rule you have is actually pretty clean, if a bit hard to follow. You could use a switch statement with indentation and spacing instead. It might be a little easier to follow and slightly faster than multiple if/elseif/else's.when HTTP_REQUEST { Parse the HTTP host header value switch [string tolower [HTTP::host]] { Host was site1 site1.domain.com { Set the pool to site1 pool pool pool_site1.domain.com } Host was site2 site2.domain.com { if {[HTTP::path] eq "/"}{ HTTP::uri "/pathtosite/site2" } if { [matchclass [IP::client_addr]/16 equals $::mySubnets] }{ pool pool_site2.domain.com_internal } else { pool pool_site2.domain.com } } Host was site3 site3.domain.com { pool pool_site1.domain.com } Unknown host, reset the connection default { reject } } }
- John_LeMay_1062
Nimbostratus
Thanks Aaron, it looks like my spacing and indentation were lost when I posted. Using the switch statement certainly looks better and will likely be easier to maintain. I'll give that a shot. - John_LeMay_1062
Nimbostratus
Interesting issue. Are comments not allowed within the switch structure? I was unable to save until I removed all comment lines after "swtch {". - hoolio
Cirrostratus
Sorry about that. I didn't test the syntax first. You can't have comments in the switch cases:when HTTP_REQUEST { Parse the HTTP host header value switch [string tolower [HTTP::host]] { site1.domain.com { Host was site1, set the pool to site1 pool pool pool_site1.domain.com } ...
- John_LeMay_1062
Nimbostratus
Nice, at least we confirmed it. I looked all over for syntax here and on ask but didn't get anywhere. Should have looked at tcl documentation. Now I know how to go back and add comments as well.
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