Forum Discussion

Joseph_Wright_1's avatar
Joseph_Wright_1
Icon for Nimbostratus rankNimbostratus
May 03, 2005

help with basic cookie iRule

I need to create an irule that parses a cookie for 'doSession=winapp01' or 'doSession=winapp02' or 'doSession=winapp03' and have it load balance to that server (for example doSession=winapp03 goes to server winapp03). Can someone help me with the syntx??

 

 

Thanks

 

  • If you do a search on HTTP::cookie in the forums, you should find several examples of accessing cookies.

     

     

    Here's a simple script that looks for the cookie 'doSession' and assigns the connection to the specified target server. Keep in mind that the target_server here needs to be an internal ip address of a given backend server (ie x.x.x.x format).

     

     

     when HTTP_REQUEST { 
        set target_server [HTTP::cookie doSession] 
        if { "" ne $target_server } { 
          log "Target Server '$target_server' specified." 
          node $target_server 
        } else { 
          log "No target server specified" 
        } 
      }

     

     

    You might also want to create an external class with valid servers to make sure you handle improper cookie values and don't try to forward to a node that doesn't exist.

     

     

    Also, if you want to keep your internal addresses private, you might want to create a mapping in your rule from your winapp01/02/03 names to internal addresses.

     

     

     when HTTP_REQUEST { 
        set target_server [HTTP::cookie doSession] 
        if { "winapp01" eq $target_server } { 
          log "Target Server '$target_server' specified, forwarding to 10.10.10.1." 
          node 10.10.10.1; 
        } elseif { "winapp02" eq $target_server } { 
          log "Target Server '$target_server' specified, forwarding to 10.10.10.2." 
          node 10.10.10.2; 
        } elseif { "winapp03" eq $target_server } { 
          log "Target Server '$target_server' specified, forwarding to 10.10.10.3." 
          node 10.10.10.3; 
        } else { 
          log "No target server specified" 
        } 
      }

     

     

    I'll leave it as an exercise to modify this code by extracting the xx from winappxx and dynamically building a node address of 10.10.10.xx to route to.

     

     

    Also, keep in mind that in case you need to dynamically control the cookie target values, you could use cookie persistence to do this for you.

     

     

    -Joe