Forum Discussion

JimB_43720's avatar
JimB_43720
Icon for Nimbostratus rankNimbostratus
Apr 14, 2011

Change destination port based on hostheader

I wondered if I could use an irule to change the destination port of a standard http request based purely on the hostheader?

 

 

The current setup is as follows:

 

 

We are using BIG IP 9.4.4.

 

 

Standard http requests for multiple sites are sent to a single vserver, (DNS Aliases are used to conserve ip addresses). (Only port 80 is allowed for inbound traffic).

 

 

 

Is there an iRule that would acheive the following;

 

 

if the hostheader is URL_A the port should be changed to port 81 on any of the physical servers in the pool

 

 

AND

 

 

if the hostheader is URL_B then the port should be changed to port 82 on any of the physical servers in the pool.

 

 

 

Cheers

 

 

Jim

 

 

 

8 Replies

  • James_Quinby_46's avatar
    James_Quinby_46
    Historic F5 Account
    One way to do this would be with 2 separate pools - one for the the port 81 apps, and the other for the port 82 apps. Then you could do something like this:

    
    when HTTP_REQUEST {
           switch [string tolower [HTTP::host]] {
               www.foo.com { pool port_81_servers }
               www.bar.com { pool port_82_servers }
           }
    }
     

  • Cheers for the reply jquinby.

     

     

    It does make sense, however being new to F5s, I'm not sure how to make multiple server pools available to a single virtual server.

     

     

  • James_Quinby_46's avatar
    James_Quinby_46
    Historic F5 Account
    Jim - all you need to do is create the virtual, the two pools and apply the iRule. It's not necessary for a VS to have a pool assigned. One of the mainstay, canonical rules is an HTTP to HTTPS redirect. It's applied to a VS listening on port 80, issues a 302 redirect to the same host on port 443, and has no additional resources applied beyond the iRule.

     

     

  • Cool.

     

     

    So if I set a default pool, all that means is that if the irule doesn't have amatch for an inbound request the traffic will be directed to the deault pool rather than any of those specified in the iRule?
  • James_Quinby_46's avatar
    James_Quinby_46
    Historic F5 Account
    Posted By JimB on 04/14/2011 08:07 AM

    Cool.

    So if I set a default pool, all that means is that if the irule doesn't have amatch for an inbound request the traffic will be directed to the deault pool rather than any of those specified in the iRule?

    In that case, you could do something like this:

    when HTTP_REQUEST {
           switch [string tolower [HTTP::host]] {
               www.foo.com { pool port_81_servers }
               www.bar.com { pool port_82_servers }
           }
    
           default { pool whatever_your_default_pool_is }
    }

     

     

  • Cheers Jquimby that worked a treat after I dropped the default pool line.

     

     

    FYI - I dropped the default pool line as the F5 returned an undefined function error.
  • A closing brace was just on the wrong line. You could also dynamically get the VS default pool name rather than hard coding it in the iRule:

    
    when CLIENT_ACCEPTED {
    
        Save the name of the VS default pool before it's modified in this iRule
       set default_pool [LB::server pool]
    }
    when HTTP_REQUEST {
       switch [string tolower [HTTP::host]] {
          www.foo.com { pool port_81_servers }
          www.bar.com { pool port_82_servers }
          default { pool $default_pool }
       }
    }
    

    Aaron