Forum Discussion
Matt_108113
Nimbostratus
Jul 15, 2009Using Irules to mimic apache Virtual hosts
I would like to use I rules to mimic Apache Virtual Hosts.
I have a web server that has a bunch of sites on it and instead of setting up virtual hosts on Apache I wanted to see if I could do this with Irules.
Basically when someone hits the site and requests http://webserver1.domain.com/SITE1 they would be redirected to.
http://SITE1.domain.com in the browser.
there are multiple sites and they are added all the time so if I could get something generic like.
when HTTP_REQUEST {
if { [HTTP::uri] contains "/SITE1" }{
HTTP::redirect http://site1.domain.com}
}
Is kind of the idea, but how to I make the F5 aware that site1 is on the webserver at http://webserver1.domain.com/SITE1?
The problem is I don't know syntactically what needs to be were to do this.
Any help would be appreciated.
11 Replies
- JRahm
Admin
Yep, that's doable. Do you have a requirement of the name (ie. webserver1), or do you want to redirect even if it's something like just-trying.domain.com/SITE1? Also, does the case of the SITE1 matter? Do you have specific sites in mind, or will the site always be the first word in the path? - Matt_108113
Nimbostratus
So this Virtual server on the F5 is a one to one ratio so I am not load balancing or anything so I don't need the webserver name as a requirement, so if they hit.
http://webserver.domain.com/SITE1 -> SITE1.domain.com
http://webserver.domain.com/SITE2 -> SITE2.domain.com
don't need to keep the webserver name, and any site that is setup on this host should redirect and if they enter something that is not there I am good with it just being non-existent as this is a backend server that 95% of the time gets queried by redirects from other sites and not direct from users on the internet.
Thanks,
Matt - Sebas_82058
Nimbostratus
Hi guys, I am working with Matt on this issue and the issue is this.
We have Apache running on one server and we have different sites under different paths.
Eg:
www.apacheserver.com/site1
www.apacheserver.com/site2
www.apacheserver.com/site3
And then on the F5 side we need to create virtual servers pointing to the Apache like this:
F5 side Apache side
www.site1.com www.apacheserver.com/site1
www.site2.com www.apacheserver.com/site2
www.site3.com www.apacheserver.com/site3
On the browser side it should look like this:
This file:
www.apacheserver.com/site1/index.html
Should be:
www.site1.com/index.html
It would be like similar to setting up a ServerRoot to /site1.
Hope this helps to explain the problem a little more. - hoolio
Cirrostratus
I'm guessing you don't want to redirect the client. If you did, the client would see the change, try to resolve www.apacheserver.com to an IP address and then make a new request to that IP. To rewrite the host header and URI you could use an iRule like this:when HTTP_REQUEST { log local0. "[IP::client_addr][TCP::client_port]: New request to [HTTP::host][HTTP::uri]" switch [string tolower [HTTP::host]] { "www.site1.com" { Rewrite host to www.apacheserver.com HTTP::header replace Host "www.apacheserver.com" Prepend /site1 to the URI HTTP::uri "/site1[HTTP::uri]" } "www.site2.com" { Rewrite host to www.apacheserver.com HTTP::header replace Host "www.apacheserver.com" Prepend /site2 to the URI HTTP::uri "/site2[HTTP::uri]" } "www.site3.com" { Rewrite host to www.apacheserver.com HTTP::header replace Host "www.apacheserver.com" Prepend /site3 to the URI HTTP::uri "/site3[HTTP::uri]" } default { Take some default action? } } } when HTTP_REQUEST priority 501 { This event is added for debug only. It bypassed the variable caching for HTTP::host and HTTP::uri log local0. "[IP::client_addr][TCP::client_port]: Updated request to [HTTP::host][HTTP::uri]" }
If the application uses absolute references to www.apacheserver.com in response headers and/or content you could rewrite them using 'HTTP::header replace' and/or a stream profile and STREAM::expression based iRule, respectively.
Aaron - Sebas_82058
Nimbostratus
Great! Let me try this out.
Thanks for your help. - Sebas_82058
Nimbostratus
Awesome! This works great!
Just in case someone needs it in the future, this would replace the following configuration in Apache.
DocumentRoot /path/to/root
ServerName www.site1.com
Thanks a lot.
Sebastian - hoolio
Cirrostratus
Are you actually filtering on the requested host header value? If not, you could remove the HTTP::header replace Host command from the iRule.
Also, once you're done testing it would be good to comment out the logging and the HTTP_REQUEST 501 event.
Aaron - Sebas_82058
Nimbostratus
No, there are no filters in place. I will remove it and test. - Bill_Callahan_8
Nimbostratus
Can you set a variable based on the HTTP::host and use that variable so you don't have to specify each site?
Something like:
set sitedir [string tolower [HTTP::host]]
HTTP::header replace Host "www.apacheserver.com"
Prepend /site1 to the URI
HTTP::uri "/sitedir[HTTP::uri]" - Sebas_82058
Nimbostratus
Removed it and also removed the lines for debugging.
Thanks again for your help.
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
