Select pool based on HTTP host header

Problem this snippet solves:

This rule was designed for a customer that had many websites hosted on one VIP. This rule will dynamically assign a pool name based on the host header sent. In this case, the admin will only have to add a pool named, for example, www.test.com_pool instead of adding every possible host header value to a switch or if/then statement in the Irule.

Code :

when HTTP_REQUEST {
    set hostpool [string tolower [HTTP::host]]_pool
    # Show us what the client sent in the host header - Turn this OFF in production
    log local0. "pool is $hostpool"
    # Use if/then with "Catch" to account for any bad host headers - otherwise TCL will throw an error to the client
    if { [catch {pool $hostpool} exc] } {
        # If a client sends a host header that does not match a pool, send to default pool
        pool P3
    }
    
}
Published Mar 18, 2015
Version 1.0

Was this article helpful?

2 Comments

  • this is great! I'm looking to do something like this in front of a cloud foundry deployment, which i think this can solve!

     

  • This is exactly what am looking for as I am about to start an implementation requiring this. however, I don't understand how this takes care of the actual load balancing or selection of pools according to the host header sent.

     

    If I have pools abc.com_pool, dbx.com_pool and xyz.com_pool and I want the corresponding client requests sent to the matching pool, how does this solution address that?

     

    Another concern is, what value does the [HTTP::host] return for host value? How do requests for both and abc.com handled?