Forum Discussion

hlayson's avatar
hlayson
Icon for Nimbostratus rankNimbostratus
Dec 10, 2019

irule, pool persistence based on cookie value

I'm hoping you guys can help me out. I have fumbled my way through this for weeks now. I'm trying to write an irule that routes to a different pool based on the value of a cookie.

 

#1 https://myapp.domain.com/alpha

#2 https://myapp.domain.com/alpha?arg=A

#3 https://myapp.domain.com/alpha?arg=B

 

The app by default should go to #1. I'm having to pass an argument in browser as workaround to test hitting against another pool. The app does several 302 redirects before the page loads, so I lose the (?agr= ) in uri after the initial execution. The first part works fine. It sends to the correct pool based on the arg set. It's on the redirects that have the issue. Since the arg= is no longer set it falls into the last else. I'm checking for the cookie, but it is not there anymore and falls to the default.

 

 

when HTTP_REQUEST {

  

  set webUri [HTTP::uri]

  set webHost [ string tolower [HTTP::host] ]

 

  HTTP::header insert X-Forwarded-For [IP::remote_addr]

  HTTP::header insert X-Forwarded-Host [HTTP::host]

  

   if { ($webUri ends_with "arg=1") } {

     set orig_cookie [HTTP::cookie value "valueA"]

     pool POOL1

     return

   }

   elseif { ($webUri ends_with "arg=2") } {

     set orig_cookie [HTTP::cookie value "valueB"]

     pool POOL2

     return

   }

   else {

      if { ( [HTTP::cookie exists cookie] ) } { 

       set new_cookie [HTTP::cookie value orig_cookie]

       return

      }

      else {

       set new_cookie [HTTP::cookie value null]

       }

      }

   }  

    switch $new_cookie {

     "valueA" {

      pool POOL1

      return}

     "valueB" {

      set tls no

      pool POOL2

      return}

     default {

      pool POOL3

      return}

   }

 

 when HTTP_RESPONSE {

  set node [IP::server_addr]:[TCP::server_port]

  set nodeResp [HTTP::status]

 

#  log connection info

  log local0. [LB::server]

  log local0. $node

  log local0. $nodeResp

}

1 Reply

  • Hi,

     

    Have you first considered using universal persistence.

    https://support.f5.com/csp/article/K7392

     

    When you say "to a different pool based on the value of a cookie", how is value of the cookie set up at the first place ?

     

    The idea would be to insert a cookie in the first response "poolselect" set to A, B, C. And then persist on that cookie value.

     

    Yoann