The first post has some conceptual suggestions. If anyone has more detail to add, please do so. Else, I’ll try testing in the next few days and see if I can provide more info on how to rewrite the URI in the request and the HTML content in the response.
All you’d need to do is re-write the hostname when the client is making a request to the server. The server would then return a Location that includes that modified hostname, and subsequent requests should be sent to the address in the Location header.
So all you should really need to do for this particular scenario is:
when HTTP_REQUEST {
if { not ( [HTTP::host] starts_with "www." ) } {
HTTP::header replace Host "www.[HTTP::host]"
}
}
Since you want the client to see the updated URL, you shouldn’t have to do any further work. The Location header sent back from the server should include the www, and the client should update appropriately. If you wanted to hide the re-write from the client, that’s where more work on the response side of things would come in.
It’s been awhile but I’m back on this. I tried the above iRule but it doesn’t seem to rewrite the hostname. Here is the rule I’m using and some log entries that show it’s not rewriting the hostname:
I’ve seen this before as well and I believe it just has to do with variable caching within the iRules engine. The header insert is taking place but it isn’t updating the value of the HTTP::host variable. Have you verified that the host field being passed to your backend webserver isn’t correct? I bet your server logs will show your rewritten value being sent.
I checked the backend with a Sniffer and the host header is being re-written. Unfortunetly I don’t think this is going to be sufficient for what we trying to accomplish.
-------------------------
There is more to it than this but we would need to be able to redirect (based on some critera) a percentage of new requests (i.e. for the default page) that come into a particular Virtual DIRECTLY to another Virtual. Is this possible? We don’t want to do client redirects and we don’t want to rewrite the host header since that request which we want to go directly another virtual will first need to hit the Virtual-A pool.
As a note we do this do distribute percentages of new sessions from our our primary Virtual to 2 other Virtuals which are used to test code changes, new products, etc.
For example for 3 Virtuals:
Virtual-A: www.abc.com (main virtual - gets 100% of new connections)
Is there a reason the traffic MUST hit the other virtuals, or do you just need it to appear that it is? You can create multiple pools that reside behind a single virtual server, using an iRule to switch as necessary. You could use the session table to count current connections, and switch new connections to each pool accordingly with some math processing in your iRule. There are a lot of options…
I see your point about using pools in lieu of doing a redirects to other Virtuals. I’ll need to check on whether that would be acceptable but let’s suppose that it is, would the following be doable using an iRule:
Assume we have a Virtual (Virtual-A) and 3 pools (pool-A, pool-B, and pool-C)
1. A request comes into the Virtual-A for www.abc.com
2. If the cookie “cookieB” exists then sendto pool pool-B
3. If the cookie “cookieC” exists then sendto pool pool-C
4. No cookie was found as indicated. Now determine if this is a NEW session to www.abc.com e.g. someone typed in their browser "www.abc.com / someone hitting the default page, etc. What criteria determines a new session?
5. If it’s not a new connection send to pool pool-A
6. If it is a new session then we want to determine, based on percentages of traffic, which pool to send this new session to. As an example to accomplish this we would want to do the following (you may have a better way to do this):
a. Run a Random Number Generator that returns a value between 1 and 100.
b. If the value > 10 then send to pool pool-A (e.g. we want 90% of new sessions go to pool-A) - exit
c. If the value is >=7 the sendto pool-B (e.g. we want 4% of new sessions go to pool-B - Drop a cookie “pool-B” (for future returning traffic) and may want to rewrite the host header - exit
d. Else send to pool-C (remaining value 1-6) so 6% of new sessions go to pool-C - Drop a cookie “pool-C” (for future returning traffic) and may want to rewrite the host header - exit
Click here for info on generating a random number.
I think you could configure cookie insert persistence with no expiry (session cookies) on the virtual, then look for the persistence cookie to determine if it is a “new” session or not…
I’m not sure what you mean by building in some logic to handle the fact that every request is evaluated by the rule.
I have the following rule below that I’m testing. It looks for an existing cookie and if it exists then it’s sent to the named pool which is extracted from the value field in the cookie.
If no cookie is found then it is assumed to be a new session so it distributes between three pools based on the value returned by the random number generator.
Is it a problem that every request is evaluated by this iRule? When you say I will need to build in logic to handle that what do you mean? Do you mean just make the iRule as efficient as possible? (which I have tried to do)
I think what was meant was that you need to be sure your rule works for all the different kinds of requests your VIP will be receiving…even those that have been passed through the rule once before.
I’ve been using the random number generator as recommended earlier in this thread and have found that it causes a much greater number of TCP connections on the server side. So if I run an iRule with rand as follows:
set rndnum [expr { int(100 * rand()) }]
then have some if statements such as:
if { $rndnum > 10 ] } {
pool abc
}
else {
pool xyz
}
}
I will 1 TCP connection on the clientside but many TCP conns on the server side. I’m using an IXIA traffic generator so I can see all connections. Plus I use the BigIP stats.
If I then make the following modification and set rndnum manually I’ll see one clientside and 1 server side TCP connection, as expected.
set rndnum 10
set rndnum [expr { int(100 * rand()) + 1 }]
Any explanation as to why this is happening? It’s definetly related to running rand. If I run any test at high traffic volumes I’ll huge numbers of server side connections.