Eliminate redirects

In lieu of doing http redirects in the following example basic example:

--------

Client connects to http://abc.com and is redirected to http://www.abc.com

--------

would it be possible to eliminate the redirect in some fashion. For example:

1. Rewrite the request for abc.com to www.abc.com and submit it to the www.abc.com virtual.

2. Rewrite the response so the client now sees www.abc.com

Running ver 9.x

One goal here is to eliminate the time it takes for the client to get redirected and to reconnect to a new URL.

Thank You.

Hello,

There are two similar posts:

Click here

(and was asked again here: Click here)

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.

Aaron

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.

Colin

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:

when HTTP_REQUEST {

if { [HTTP::host] equals “abc.com” } {

log local0. “orig hostname [HTTP::host]”

HTTP::header replace Host “www.[HTTP::host]”

log local0. “rewritten hostname is [HTTP::host]”

}

}

Aug 10 18:04:36 127.1.1.3 tmm1[2341]: Rule hostname_rewrite : orig hostname abc.com

Aug 10 18:04:36 127.1.1.3 tmm1[2341]: Rule hostname_rewrite : rewritten hostname is abc.com

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.

-Joe

Does the actual header that’s sent to the server get modified? I wonder if [HTTP::host] is returning the old value.

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)

Virtual-B virtb.abc.com (will get some adjustable % of the www.abc.com new sessions e.g. 4%)

Virtual-C virtc.abc.com (will get some adjustable % of the www.abc.com new sessions e.g. 6%)

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

In this case, I do think the pool chooser approach should be all you need to do though…

Before I start on an actual iRule can you help me with the following:

I’m not sure how to go about determining whether a request is a new session or not and then incorporate that into an iRule.

Also do you have available code for Random Number Generator that I can use?

Thank You.

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…

HTH

/deb

Yes the HTTP_REQUEST event will fire on every connection to the virtual so you will need to build in some logic to handle that.

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)

Thanks.

===================================================

when HTTP_REQUEST {

if { [HTTP::cookie exists “stick2”]} {

existing cookie found so not a new session. Send to correct pool based on cookie value

pool [HTTP::cookie “stick2”]

}

else {

no existing cookie (new session). Distribute between 3 pools using random number (1-100)

set rndnum [expr { int(100 * rand()) }]

if { $rndnum > 9 } {

pool www1

approx. 90% to pool www1

}

elseif { $rndnum >=5 } {

pool www2

approx 5% to pool www2

}

else {

pool www3

remaining 5% to pool www3

}

}

}

Is this iRule inefficient in terms of looking at all requests? How can it be made more efficient?

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.

Colin

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.

Thanks.