Forum Discussion

Ian_J_37801's avatar
Icon for Nimbostratus rankNimbostratus
Feb 25, 2008

Cookie persistence




I am using cookie persistence to load balance some web servers. So far so good!



What happens though is the BigIP adds the cookie to the end of the cookie list as shown below:



HTTP/1.1 200 OK


Date: Mon, 25 Feb 2008 12:31:16 GMT


Server: Apache


Set-Cookie: PHPSESSID=7f596726c038e1a8773e86af878e438c; path=/;


Expires: Thu, 19 Nov 1981 08:52:00 GMT


Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0


Pragma: no-cache


Set-Cookie: familyFilter=5; path=/;


Set-Cookie: User=%7B%22LEID%22%3A356%2C%22LangID%22%3A%22en%22%2C%22sessionSaver%22%3A%22%22%2C%22sessionsCounter%22%3A1%7D; expires=Sat, 23-Feb-2013 12:31:16 GMT; path=/;


Set-Cookie: User=%7B%22LEID%22%3A356%2C%22LangID%22%3A%22en%22%2C%22sessionSaver%22%3A%22%22%2C%22sessionsCounter%22%3A1%2C%22rC%22%3A%7B%22today%22%3A1%7D%7D; expires=Sat, 23-Feb-2013 12:31:17 GMT; path=/;


Set-Cookie: md=%7B%22LEID%22%3A356%2C%22langID%22%3A%22en%22%2C%22visitID%22%3A%222f516862574db006fc4acc32fa151d2d%22%2C%22pve%22%3A1%2C%22sessionsCounter%22%3A1%2C%22filters%22%3A5%2C%22countryCode%22%3A%22GB%22%2C%22headerLangID%22%3A%22en%22%2C%22userLocations%22%3A%5B242%5D%2C%22userLanguages%22%3A%5B9%5D%7D; expires=Sat, 23-Feb-2013 12:31:17 GMT; path=/;


Vary: Accept-Encoding,User-Agent


Content-Type: text/html


Set-Cookie: BIGipServerDSA_WWW_3.32.1.2=27462211112.20480.0000; path=/


Content-Encoding: gzip


Transfer-Encoding: chunked



Unfortunately, my application needs to have this returned at the beginning of the "Set-Cookie" list as the application also queries the cookie. Is there anyway to add the LTM cookie in forst rather than last?



Many thanks







4 Replies

  • Headers (including the Set-Cookie header) are generally added to the end of the existing headers. If you want to change the order of the cookies in responses, I think you'd need to save each Set-Cookie header value in an array, remove the header and then re-insert each of the headers in your required order. This could be a bit resource intensive to do for every response.



    Can the app be modified to parse the cookie headers anywhere in the list of headers? If so, that would be the most efficient fix.



    If you do need to use an iRule to do this, what version are you running? The reason I ask is that the handling of multiple headers of the same name was changed in 9.4.0.



  • Actually, it looks like you're using a session based persistence cookie. The BIG-IP should only set this once per client session. So it might not actually be too resource intensive to check for the BIG-IP persistence cookie and only reorder the Set-Cookie headers if the BIG-IP persistence cookie is present.



    Are you running 9.4.0 or higher?



  • Hi Aaron,



    Many thanks for the response.



    I am using 9.4.3








  • Hello Ian,

    I was thinking that you'd need to use an array, but that assumed that you could delete one header at a time. The HTTP::header remove command deletes all headers of that name though. So I used a list instead.

    Can you give this a shot?

    when RULE_INIT {
        Set the name of the cookie to re-order the Set-Cookie headers for
       set ::bigip_persist_cookie_name "BIGipServerDSA_WWW_3.32.1.2"
       set ::bigip_persist_cookie_name "lastcookie"
        Log debug messages (to /var/log/ltm)?  0=no, 1=yes.
       set ::set_cookie_reorder_debug 1
    when HTTP_RESPONSE {
        Check if the response contains the BIG-IP persistence cookie
       if {[HTTP::cookie exists $::bigip_persist_cookie_name]}{
          if {$::set_cookie_reorder_debug}{log local0. "Found persist cookie.  Re-ordering Set-Cookie headers"}
           Save the Set-Cookie header values to a list
          set set_cookie_values [HTTP::header values Set-Cookie]
          if {$::set_cookie_reorder_debug}{log local0. "Values: $set_cookie_values"}
           Remove the headers
          HTTP::header remove Set-Cookie
           Get the list index of the BIG-IP cookie's Set-Cookie header
          set bigip_cookie_index [lsearch -glob $set_cookie_values ${::bigip_persist_cookie_name}*]
          if {$::set_cookie_reorder_debug}{log local0. "BIG-IP cookie index: $bigip_cookie_index"}
           Add back the BIG-IP cookie set-Cookie header first
          HTTP::header insert Set-Cookie [lindex [lrange $set_cookie_values $bigip_cookie_index [expr $bigip_cookie_index + 1]] 0]
          if {$::set_cookie_reorder_debug}{log local0. "Inserted BIG-IP cookie: [lindex [lrange $set_cookie_values $bigip_cookie_index [expr $bigip_cookie_index + 1]] 0]"}
           Add back all Set-Cookie values except the BIG-IP cookie
          for {set i 0} {$i < [llength $set_cookie_values]}{incr i}{
              Check that this isn't the BIG-IP cookie Set-Cookie value 
             if {$i != $bigip_cookie_index}{
         Insert the header
        HTTP::header insert Set-Cookie [lindex [lrange $set_cookie_values $i [expr $i + 1]] 0]
        if {$::set_cookie_reorder_debug}{log local0. "Inserting: [lindex [lrange $set_cookie_values $i [expr $i + 1]] 0]"}
           Clear the list of saved headers
          unset set_cookie_values