Forum Discussion

Abed_AL-R's avatar
Abed_AL-R
Icon for Cirrostratus rankCirrostratus
Aug 28, 2020

iRule | route to another pool according to URI

Hello guys

I'm trying to write an iRule that route the traffic if the end user hit a specific URI. This specific URI has a pattern inside it and according to this pattern I'm doing the match.

But here is the situation:

1- That URI is changed after the user click on any link inside the page and then the F5 re-route him again to the VS default pool.

I need the user to persist all the time to the specific pool the iRule route him to.

2- If the client tries again to different "branchcode" , then route to the default pool.

Here is what I wrote at first but not working:

when HTTP_REQUEST {
        if { [URI::query [string tolower [HTTP::uri]] "branchcode"] equals "123"} {
            log local0. "my_pool was selected"
            pool my_pool
        }
        else {
            pool default_pool
        }
}

It worked only at the begining when the user hit the URI with "branchcode" 123, but then if he click on any link inside the webpage, he's being redirected to the VS default pool , because the URI doesn't has the "branchcode" inside it anymore.

And then I added the:

persist source_addr

Although it should take it from the VS but didn't work also.

And then I tried to change it to this one:

when HTTP_REQUEST {
        if { [URI::query [string tolower [HTTP::uri]] "branchcode"] equals "123"} {
            persist source_addr 720
            set static::myclientip [IP::client_addr]
            pool my_pool
        }
     elseif { [info exists "static::myclientip"] } {
           pool my_pool
      }
}

But the thing is that after I wrote this iRule all users are being routed to the specific pool "my_pool". which I don't understand why.

Looks like the elseif always matches .....

Also, if the user closes the page or manually change the branchcode to something else.. he still hit the "my_pool" , i think because the persistence timeout has not reached the 720 seconds..

Anyone please can help to tune the iRule and modify it?

Thanks

2 Replies

  • Set a cookie for the client that specifies the current branchcode.

    Use the cookie value to determine what pool the client request is sent to.

    When they make a request that changes the branchcode, change the cookie value to the new branchcode.

     

    You could use the client IP and a subtable, but for HTTP a cookie is the appropriate mechanism for maintaining state.

     

    Here is some example code - it works as expected, but you should check it out and adjust it to your requirements.

     

    when HTTP_REQUEST {
      set new_branchcode ""
      log local0. "branchcode is [URI::query [string tolower [HTTP::uri]] "branchcode"]"
      switch [URI::query [string tolower [HTTP::uri]] "branchcode"] {
        123 {
           HTTP::cookie remove "branchcode"
           HTTP::cookie insert name "branchcode" value "123"
           log local0. "updating branchcode cookie to 123"
           set new_branchcode "123"
        }
        456 {
           HTTP::cookie remove "branchcode"
           HTTP::cookie insert name "branchcode" value "456"
           log local0. "updating branchcode cookie to 456"
           set new_branchcode "456"
        }
      }
     
      log local0. "selecting the pool based on branchcode cookie [HTTP::cookie value {branchcode}]"
      
      switch [HTTP::cookie value {branchcode}] {
        123 {
          # pool 123_pool
          log local0. "pool is 123_pool"
        }
        456 {
          # pool 456_pool
          log local0. "pool is 456_pool"
        }
        default {
          # select default pool
          log local0. "pool is default pool"
        }
      }
    }
     
    when HTTP_RESPONSE {
      if {![string equal $new_branchcode ""]} {
        log local0. "sending a new cookie $new_branchcode"
        HTTP::cookie insert name "branchcode" value $new_branchcode
      }
    }

     

    • Abed_AL-R's avatar
      Abed_AL-R
      Icon for Cirrostratus rankCirrostratus

      Thanks

      I'll check it out.

      In the mean time I provided alternative solution - with redirection to another VS the has different pool according to data group value

      when HTTP_REQUEST {
          if { [class match -- [URI::query [string tolower [HTTP::uri]] "branchcode"] equals branchcode_Data ] } {
                 HTTP::redirect https://qa.mywebsite.com/[HTTP::uri]
               }
      }

      But definitely I will check your iRule to see if it answers our needs.

      Much thanks to you. 🙂