Using 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.

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?

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

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:

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:

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.

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

Great! Let me try this out.

Thanks for your help.

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

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

No, there are no filters in place. I will remove it and test.

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]”

Removed it and also removed the lines for debugging.

Thanks again for your help.

@bcallahan, yes you could do something like this:

[getfield [domain [HTTP::host] 2] 1]

or

[lindex [split [HTTP::host] .] end-1]

Either of these would take string a string value from HTTP::host of www.site1.com and return only site1

So the rule could be consolidated to:

when HTTP_REQUEST { 
   HTTP::header replace Host "www.apacheserver.com" 
   HTTP::uri "/[getfield [domain [HTTP::host] 2] 1][HTTP::uri] 
 }

With this approach, you might build a class of acceptable websites to compare against to keep noise from hitting your servers. Same principle as Hoolio suggested with the default case in the switch.

HTH…Jason