sample irule for redirect / rewrite

BIG-IP VE (Virtual Edition) v10.2.4 ( build 577.0 )

Could someone please provide or point me to an irule that performs redirects / rewrites , including the match class and associated data-file.

Thanks.

I’ll assume that this is for incoming HTTP requests and that you’re running at least BIG-IP version 10 or higher. The old find/matchclass commands have been deprecated. Also, for simplicity sake I’ll assume you want an “internal” data group.

1. Create a data group. In the management GUI, go to iRules and then the Data Group tab. Create a data group of type String and start entering values. The first block is the “key” and the second block is the corresponding “value”. So for example, let’s say you want to do a simple redirect based on the incoming URI. Here’s what the data group might look like:

A sample data group called “my_test_datagroup”:

“/foo” := “foo.example.com”

“/bar” := “bar.example.com”

“/test” := “blah.example.com”

2. With the data group built, you’re iRule might look something like this:

when HTTP_REQUEST {
    if { [class match [string tolower [HTTP::uri]] starts_with my_test_datagroup] } {
        HTTP::redirect "http://[class match -value [string tolower [HTTP::uri]] starts_with my_test_datagroup]"
    }
}

Using the above iRule, if a user makes an initial request to, for example, “http://www.example.com/foo”, he’ll get an immediate redirect to “http://foo.example.com”. The first class match command checks to see if the key (in this case the requested URI) is in the data group (URI starts_with “/foo”). The operator could be anything from “starts_with”, “ends_with”, “contains”, “equals”, and others encompassing any number of conditions. Also notice that the data group does not have to be in quotes. The second class match command then takes the value from the record that contains the matching key and inserts that into the redirect string (“foo.example.com”).

This is obviously an over simplistic example because I don’t know your exact requirements. Take a look at the class command wiki page for more detailed information on what’s possible.

Thanks for the response Kevin.

What are my options if my data-group has a very large number of key-value pairs ( thousands ) ?

I understand that I can create a class/data-group that points to an external file which just lists the key-value pairs in the format you’ve indicated ( key := value ).

But how will this impact performance ? I assume f5 loads the data-file into memory ? But when does this happen ? On f5 startup ? Or the first time an iRule references the class/data-group ?

Thanks again.

Thanks for the response Kevin.

What are my options if my data-group has a very large number of key-value pairs ( thousands ) ? This amount of data would seem to be unwieldy to manage via the web-admin interface or the CLI.

I can point my class/data-group to an external-data-file which contains the key-value pairs.

But what is the performance impact of storing data in a file ? I assume f5 must load the data into memory and store it as a hash ( or performance would be hopeless ) but when does this happen ? On f5 startup ( at the time bigip.conf is loaded ) ? Or does the load occur the first time an iRule needs to match against the associated class/data-group ?

Thanks again.

What are my options if my data-group has a very large number of key-value pairs ( thousands ) ?i understand thousands is fine.

I assume f5 loads the data-file into memory , but when does this happen ? On f5 startup ? Or the first time an iRule references the class/data-group ?if i am not wrong, in 10.x it is when irule is changed. in 11.x, it happens when external file is updated (through gui or tmsh).

Forcing a reload of External Data Groups within an iRule by Joe Pruitt

just my 2 cents.

In the comments to this article :

one poster says they encountered a 100k limit for an external-data-file.

Does anyone know if such a limit exists in v10.2.4 ?

Does anyone know if such a limit exists in v10.2.4 ?i understand it is limited by system resource (i.e. it is not hard limited).

anyway, there is bug which is fixed in 11.4.0.

ID284893 - large external class causes a hang and reboot

The limitation is in fact resource-based, and up to v11.4 (v10 - v11.3) the practical limit is about 100k records in an external data group. I’d need to check, but I believe 11.4 introduces support for millions of records in a data group. If you need to support more than 100k records now, you could conceivably load all of the data into the session table using a “services VIP”.

But what is the performance impact of storing data in a file ? I assume f5 must load the data into memory and store it as a hash ( or performance would be hopeless ) but when does this happen ? On f5 startup ( at the time bigip.conf is loaded ) ? Or does the load occur the first time an iRule needs to match against the associated class/data-group ?

The answer to this is “on update”, and that there is no appreciable difference in performance between an internal and external data group, both hashed and consumed into memory when loaded.

Interesting stuff.

In the redirect iRule , is it necessary to change the host headers ?

Also, what would an sample iRule for a rewrite look like ? Again, need to change the host headers ?

Well, in a redirect iRule, and especially the one above, the client (browser) would change the host header for you. In fact the host header might only ever need to be changed en route to the server if the server expects (and requires) a different host header than what the client is providing. It’s a little difficult to give you any sort of solid example without knowing what you’re trying to achieve. In any case, think of a data group as a set of key-value pairs, where the key is usually something the client is providing (host name, URI part, cookie, anything really), and the value is what you want to translate that to. The above example took the request URI and translated that into a host name for a physical (302) redirect. It could as easily have been a translation to a different URI, both explicitly through a redirect, or by simply changing the URI (or part of it) in line, without the user’s knowledge.

What capabilities does f5 BIG-IP offer for performance testing of iRules ?

In my case, my iRule must ultimately handle thousands of redirects/rewrites, but that that server backend infrastructure is not yet present. So, I need more of a proof-of-concept that f5 BIG-IP can handle this level of traffic.

MODS: Please note that I attempted to post to the Design and Configuration > Performance Testing forum , but I can’t even go to that forum -- I get an error infinite redirect loop ( so ironic that perf test forum has an infinite redirect loop error ! )

Good question, and difficult to give a definitive answer. Here are a few options though:

1. The timing command in iRules: DevCentral

2. I’m a big fan of both Apache Jmeter and LoadUI (both free) for local stress testing.

3. A cool document on performance testing methodology: DevCentral

4. Performance testing with HTTPWatch: DevCentral

5. There are a few testing frameworks out there. I can’t recommend any because I haven’t used them.

I’d also ask, are these redirects for a single application, or many?

Scenario is a single monolithic site being broken-up into micro-sites - but not in one fell swoop, so all sites must co-exist for a period of time.

Micro-sites are deployed with natural-language urls that each are mapping to an old-style url that is handled by the monolithic site server.

So the need for redirects is motivated by SEO.

In addition to redirects, also have need perform invisible rewrites of the natural language URLs to the the old-style URLs ( handled by the monolithic site ).

QUESTION : how exactly should I perform the rewrite ? I’ve seen rewrite iRules written like this :

when HTTP_REQUEST {

if { [class match [string tolower [HTTP::uri]] starts_with rewrites_datagroup] } {

HTTP::uri “http://[class match -value [string tolower [HTTP::uri]] starts_with rewrites_datagroup]”

}

}

But what I find strange is that the apparently there’s no need to specify the destination pool … ?

So how exactly does the request find its way to the pool ?

The pool in your example is explicitly declared in the virtual server configuration. The HTTP::uri command simply rewrites the incoming URI on the way to the server.

Ok so if virtual server has an assigned default pool , then just set uri. But if no default pool assigned , must also direct to a pool , correct ?

That is correct.

I successfully tested my redirect/rewrite irule against data-groups with external data-files of 100 , 1K , 10K records …

However, when I changed my data-group to point to a 100K record file , it crashed f5 !

Here’s what happened : I had an existing data-group pointed to a 10K record file , which I re-pointed to a 100K record file. I clicked “Update” and the file upload process began but ultimately hung. Next the browser admin page crashed with the “lost connection” message. Attempts to re-invoke the browser admin failed similarly.

Rebooting Linux didn’t help much. The browser admin page was very slow to load and ultimately displayed a banner message “General database error retrieving information”. I was able to navigate the menus but when I clicked the data-group the browser admin hung and crashed just as before.

Next , I modified bigip.conf , changing the data-group to point to the 10K record file. Reboot, but not luck.

Next, I modified bigip.conf , removing all references to external data-files ( deleted data-groups , deleted irule , modified vip to not use irule ). Reboot, but no luck.

Possibly, BIG-IP stores startup config instructions and/or sequences in its db which is now in an unstable state -- potentially telling f5 on startup to attempt to load the 100K record file ? Or how could my actions have put f5 in an unstable state ?

How to recover my f5 instance ? Or is it totally trashed ?

My understanding is that v10.2.4 should support load/cache of a 100K record data-file -- is this correct ?

Thanks for any help.