Forum Discussion
Need help with Irule to redirect based on http path or host header
I need to create an irule that will redirect traffic to a different pool bassed on the following criteria. The http path begins with /socket.io OR ((The http header 'Upgrade' contains the string 'WebSocket') AND (the http header 'Host' begins with 'ws')). This is what I have so far, any help is appreciated.
Assign a pool HTTP path
when HTTP_REQUEST {
switch -glob [string tolower [HTTP::path]] {
"/socket.io*" {
select the pool
pool poolA
}
elseif HTTP::request {
switch -glob [string tolower [http_host]] contains { "
19 Replies
- Richard__HarlanHistoric F5 Account
Try this, just a quick iRule thrown together
when HTTP_REQUEST { if { [string tolower [HTTP::path]] starts_with "/socket.io" } { pool poolA } elseif { { [HTTP::header "Upgrade"] contains "Upgrade" } && { [string tolower [HTTP::host]] starts_with "ws" } } { pool poolb } } - Thomas_Gobet
Nimbostratus
Just be careful, the first condition for your elseif will be :
elseif { { [HTTP::header "Upgrade"] contains "WebSocket" } && { [string tolower [HTTP::host]] starts_with "ws" } } {The mistake was just on "Upgrade" instead of "WebSocket"
- Richard__HarlanHistoric F5 AccountGood catch typed to fast. Thanks
- Thomas_Gobet_91
Cirrostratus
Just be careful, the first condition for your elseif will be :
elseif { { [HTTP::header "Upgrade"] contains "WebSocket" } && { [string tolower [HTTP::host]] starts_with "ws" } } {The mistake was just on "Upgrade" instead of "WebSocket"
- Richard__HarlanHistoric F5 AccountGood catch typed to fast. Thanks
- pvaughan
Nimbostratus
So next question if none of those conditions match will the default pool be chosen or do I have to put else default at the end of the rule?
- Thomas_Gobet
Nimbostratus
Yes you're right, if none of this conditions match your default pool will be chosen.
- pvaughan
Nimbostratus
Here is the irule I have, let me know if it looks correct.
Assign a pool HTTP path matches when HTTP_REQUEST { if { { [string tolower [HTTP::path]] starts_with "/socket.io" } { pool pl_vantage-beta_1337 Or if header string matches } elseif { { [HTTP::header "Upgrade"] contains "WebSocket" } && { [string tolower [HTTP::host]] starts_with "ws" }} { pool pl_vantage-beta_1337 } Otherwise use default pool default { pool pl_vantage-beta_http } } - Thomas_Gobet
Nimbostratus
Your last condition is not good. Default is only used when you use the "switch" parser instead of multiples if, elseif...
So you can replace default by else and it will work.
Remember that you don't need to use your last line with the else, because on your virtual server you can set a default pool.
- pvaughan
Nimbostratus
Awesome, thanks for the help guys I am going to test this out today.
- pvaughan
Nimbostratus
Here is the final irlule, when I add it to the virtual everything breaks. If you guys could take a look I would appreciate it. Thanks
Assign a pool HTTP path matches when HTTP_REQUEST { if { [string tolower [HTTP::path]] starts_with "/socket.io" } { pool pl_vantage-beta_1337 Or if header string matches } elseif { { [HTTP::header "Upgrade"] contains "WebSocket" } && { [string tolower [HTTP::host]] starts_with "ws" }} { pool pl_vantage-beta_1337 } Otherwise use default pool } - Thomas_Gobet
Nimbostratus
Everything breaks ?!
Could you tell us what you're're doing as tests ?
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* 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