Forum Discussion

Vinod_F5_212422's avatar
Vinod_F5_212422
Icon for Nimbostratus rankNimbostratus
Jul 22, 2015

Rules to redirect based on URL

Hi,

 

I have a web application that is deployed on 3 servers also 3 URL's to them

 

  • Server 1 (192.XXX.XXX.XXX) ==> http:\192.XXX.XXX.XXX\
  • Server 2 (192.XXX.XXX.YYY) ==> http:\192.XXX.XXX.YYY\
  • Server 3 (192.XXX.XXX.ZZZ) ==> http:\192.XXX.XXX.ZZZ\

I also have a public URL what given to me which is http://website.com/

 

I need to redirect different users to different servers based on a context on my public URL.

 

But I dont have these contexts (User1,User2,User3) on my Servers 1,2 or 3.

 

  • If the URI path is /User1, /User2 or /User3 (with nothing following), and you are using BIG-IP version 11.4 or higher, you can accomplish this using Local Traffic Policies:

    If you are using a version before 11.4, you can use HTTP Classes:

    If, instead, the URI path is, e.g., /User1/... (where ... is any arbitrary path) and you want to send everything after the /User1/ part to the server, then you'll need an iRule. Something like this:

    when HTTP_REQUEST {
        switch -glob [HTTP::uri] {
            "/User1" -
            "/User1/*" { 
                HTTP::uri [substr [HTTP::uri] 6]
                node 192.168.1.1:80
            }
            
            "/User2" -
            "/User2/*" {
                HTTP::uri [substr [HTTP::uri] 6]
                node 192.168.2.1:80
            }
            
            "/User3" -
            "/User3/*" {
                HTTP::uri [substr [HTTP::uri] 6]
                node 192.168.3.1:80
            }
        }
    }
    

    This can be made more efficient, readable and configurable with the use of a datagroup.

  • ... and in fact, I think I have a bit of a bug there with the non-glob matches, so here is a variant that uses a data group :).

    create ltm pool pool-user1-context members add { 192.168.1.1:80 { } }
    create ltm pool pool-user2-context members add { 192.168.2.1:80 { } }
    create ltm pool pool-user3-context members add { 192.168.3.1:80 { } }
    
    create ltm data-group internal dg-user-server type string records add { \
       User1 { data pool-user1-context } \
       User2 { data pool-user2-context } \
       User3 { data pool-user3-context } \
    }
    
    create ltm rule context-to-server {
    when RULE_INIT {
        set static::uc_user_server_dg dg-user-server
    }
    
    
    when HTTP_REQUEST {
        set context [getfield [HTTP::uri] / 2]
        set swpool [class lookup $context $static::uc_user_server_dg]
        
        if { $swpool ne "" } {
            set nuri [substring [HTTP::uri] [string length "/$context"]]
            if { $nuri eq "" } {
                HTTP::uri /
            } else {
                HTTP::uri $nuri
            }
            
            pool $swpool
        }
    }
    }
    

    It makes sense to define a default pool for the Virtual Server to which this rule would be applied.