Forum Discussion
Silicon_84874
Nimbostratus
Jul 12, 2010Redirection to the same pool
Hi,
I'm new to iRules and do not have any formal training and I'm looking for some assistance please.
This is what I'm trying to achieve;
I have 1 pool consisting of two servers.
The two servers/pool hosts 3 websites. For simplicity, lets call it a, b and c.
pool_1/a
pool_1/b
pool_1/c
What I'm hoping to achieve is this;
http://a.domain.com/ gets redirected to pool_1/a
http://b.domain.com/ gets redirected to pool_1/b
http://c.domain.com/ gets redirected to pool_1/c
So far this is what I managed to come up with. It's incomplete and possible not going to work, but if someone could please finish off where I started or suggest otherwise.
when HTTP_REQUEST {
if { [HTTP::uri] starts_with "a" } {
pool pool_1
} elseif { [HTTP::uri] starts_with "b" } {
pool pool_1
} elseif { [HTTP::uri] starts_with "c" } {
pool pool_1
}
}
Thanks in advance!
7 Replies
- hoolio
Cirrostratus
You could create a separate pool for each host header value and then use HTTP classes to select the corresponding pool. Or if you wanted to use an iRule you could use something like this:when HTTP_REQUEST { Check the requested host header set to lower case with wildcard matching switch -glob [string tolower [HTTP::host]] { "a.example.com" { pool pool_a } "b.example.com" { pool pool_b } "c.example.com" { pool pool_c } default { pool default_pool } } }
Aaron - Silicon_84874
Nimbostratus
Hi Aaron,
Thanks for the reply. The thing I don't quite understand is if the two servers are hosting the same 3 sites, how does the iRule differentiate between the sites?
For example;
node 1
node 2
node 1 and 2 is host to 3 websites
a.example.com
b.example.com
c.example.com
pool_a members node 1, 2
pool_b members node 1, 2
pool_c members node 1, 2
I've tried the iRule and I am having problems seeing it redirect traffic to anything but the default or root site.
I was hoping to see traffic be redirected as such;
A) User enters in b.example.com
B) Script looks for "b" then what it really does is redirects it to example.com/b
Similarly user goes to c.example.com gets redirected to example.com/c - hoolio
Cirrostratus
Do you want the client to see the change? If so, you could send them an HTTP redirect. If you want to rewrite the Host and URI, you could do that as well using HTTP::header replace Host "newhost" and/or HTTP::uri.
Here is a redirect example:when HTTP_REQUEST { Check the requested host header set to lower case with wildcard matching switch -glob [string tolower [HTTP::host]] { "a.example.com" - "b.example.com" - "c.example.com" { Redirect the client to a hardcoded URL with the first field of the requested host header appended to the URI HTTP::redirect "http://www.example.com/[getfield [HTTP::host] " " 1]" } } }
To do a rewrite of the Host and URI, you could use something like this:when HTTP_REQUEST { Check the requested host header set to lower case with wildcard matching switch -glob [string tolower [HTTP::host]] { "a.example.com" - "b.example.com" - "c.example.com" { Rewrite the URI with the first field of the requested host header appended to the URI and hardcode the Host HTTP::header replace Host "www.example.com" HTTP::uri "/[getfield [HTTP::host] " " 1]" } } }
Aaron - Silicon_84874
Nimbostratus
Thanks Aaron! You've pointed me in the right direction. I had problems getting your direct examples to work for my situation. I did however manage to use your script to get there. I probably should have been more specific and mentioned that it was for internal use only. It may have made a difference to your example?
After re-hashing a few things, this worked for me;
Thanks again for your quick response and assistance! - Silicon_84874
Nimbostratus
when HTTP_REQUEST { Check the requested host header set to lower case with wildcard matching switch -glob [string tolower [HTTP::host]] { "a.example.com" { Redirect the client to a hardcoded URL with the first field of the requested host header appended to the URI HTTP::redirect "http://www.example.com/[getfield [HTTP::host] " " 1]" "b.example.com" { HTTP::redirect "http://www.example.com/[getfield [HTTP::host] " " 1]" "c.example.com" { HTTP::redirect "http://www.example.com/[getfield [HTTP::host] " " 1]" } } } - hoolio
Cirrostratus
I think the forum code munged your post.
If the action of the switch case is the same, you can group them using the - syntax that I messed up in my post above. I edited the post above to correct the problem. Can you take a look at that and see if it works for you?
Aaron - Silicon_84874
Nimbostratus
Hi Aaron,
They both work alot better with the "-" syntax.
Thanks for your help once again.
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