Forum Discussion

F5irulelearner_'s avatar
F5irulelearner_
Icon for Nimbostratus rankNimbostratus
Aug 01, 2014

irule to read the url and store as cookie

Hello,

 

I am working to divert the traffic to a specific server based on the servername passed in the URL. Can you please help with an irule which reads the url and stores it as a cookie

 

Example:

 

www.xyz.com/download.do?app=qwa1234

 

irule should read the URI and store app=qwa1234 or qwa1234 as the cookie such that subsequent requests are routed to Same server even if qwa1234 is not part of the URI.

 

Any assistance is greatly appreciated.

 

5 Replies

  • Its easy enough. The trick is trying to tie it into the existing persistence method and let it do the leg work. The first thing you need to consider is the BIGIP sees the pool members as IP addresses. While you can specify DNS names they are only looked up at the time the member is added to the pool. Thereafter they are referred to by IP address. So either you specify the IP address or you use RESOLV::lookup command to look up the IP address. When you have that you select the pool member using something like this...

    if {not [HTTP::cookie exists "qwaapp"]} {
      if {[URI::query [HTTP::uri] app] ne  ""} {
        pool mypoolname member [RESOLV::lookup -a [URI::query [HTTP::uri] app]] 80
      }
    }
    log local0. "Cookie qwaapp is [HTTP::cookie qwaapp]"
    

    Requirement

    DNS is configured and it can resolve the name you provide in the app parameter.

    Cookie persistence applied with cookie name of qwaapp

    Initial request is made with http://www.xyz.com/download.do?app=qwa1234.mycompany.com or you can manually add the domain in the iRule.

    Instructions

    Replace mypoolname with the actual pool name.

    Replace port 80 with the actual port used by your pool members.

    When the request arrives it checks if qwaapp cookie exists. If not then checks if the app parameter is passed in the URL. If so then it selects the pool member based on the IP address of the name provided.

    • Kevin_Davies_40's avatar
      Kevin_Davies_40
      Icon for Nacreous rankNacreous
      Note this will only work properly if the initial request has the app parameter. If its not in the first request then the BIGIP will set the cookie with the pool member that it selected using the load balancing method and thereafter continue to use that setting.
  • My Existing irule is of the below format. This works fine for the first request when the qwa1234 is passed in the URL. However I need the qwa1234 to be stored as cookie and similarly qwa5678 whenever it is passed in the URI. So please advise how to set cookie for this scenarion. If possible please share the entire irule.

     

    when HTTP_REQUEST { set uri [HTTP::uri] if { ($uri ends_with "qwa1234") } { pool lpqwa1234-25046 } elseif { ($uri ends_with "qwa5678") } { pool lpqwa5678-25046 } default pool app-E2-openhome-25046 }

     

  • My complete irule would be something like below. So please advise how to store the cookie value as qwa1234 or qwa5678 whenever they are passed in the URL.

     

    URL Example: www.xyz.com/download.do?app=qwa1234

     

    when HTTP_REQUEST { set uri [HTTP::uri] if { ($uri ends_with "qwa1234") } { pool lpqwa1234-25046 } elseif { ($uri ends_with "qwa5678") } { pool lpqwa5678-25046 } else if { {$cookie equals qwa1234) } { pool lpqwa1234-25046 } else if { {$cookie equals qwa5678) } { pool lpqwa5678-25046 } default pool app-E2-openhome-25046 }

     

  • i agree with Kevin. you should use persistence method. using query parameter value as cookie value will result all request goes to only one pool member (because parameter value is same).

    e.g.

     config
    
    root@(B6900-R69-S40)(cfg-sync Standalone)(Active)(/Common)(tmos) list ltm virtual bar
    ltm virtual bar {
        destination 100.100.100.41:80
        ip-protocol tcp
        mask 255.255.255.255
        persist {
            cookie {
                default yes
            }
        }
        profiles {
            http { }
            tcp { }
        }
        rules {
            qux
        }
        source 0.0.0.0/0
        source-address-translation {
            type automap
        }
        vs-index 2
    }
    root@(B6900-R69-S40)(cfg-sync Standalone)(Active)(/Common)(tmos) list ltm rule qux
    ltm rule qux {
        when HTTP_REQUEST {
      set qry [URI::query [HTTP::uri] app]
      if { $qry ne "" } {
        switch $qry {
          "qwa1234" { pool lpqwa1234-25046 }
          "qwa5678" { pool lpqwa5678-25046 }
          default { pool app-E2-openhome-25046 }
        }
      } else {
    
        if { [HTTP::cookie exists BIGipServerapp-E2-openhome-25046] } {
          pool app-E2-openhome-25046
        } elseif { [HTTP::cookie exists BIGipServerlpqwa1234-25046] } {
          pool lpqwa1234-25046
        } elseif { [HTTP::cookie exists BIGipServerlpqwa5678-25046] } {
          pool lpqwa5678-25046
        } else {
          pool app-E2-openhome-25046
        }
      }
    }
    when HTTP_RESPONSE {
      log local0. "client [IP::client_addr]:[TCP::client_port] server [IP::server_addr]:[TCP::server_port] \
        pool [LB::server pool]"
    }
    }
    
     test
    
    [root@client3 ~] curl -I http://100.100.100.41
    HTTP/1.1 200 OK
    Date: Tue, 05 Aug 2014 10:41:58 GMT
    Server: Apache/2.2.3 (CentOS)
    Last-Modified: Sun, 09 Feb 2014 08:39:51 GMT
    ETag: "41879c-59-2a9c23c0"
    Accept-Ranges: bytes
    Content-Length: 89
    Content-Type: text/html; charset=UTF-8
    Set-Cookie: BIGipServerapp-E2-openhome-25046=1707657416.20480.0000; path=/
    
    [root@client3 ~] curl -I http://100.100.100.41/?app=qwa1234
    HTTP/1.1 200 OK
    Date: Tue, 05 Aug 2014 10:42:05 GMT
    Server: Apache/2.2.3 (CentOS)
    Last-Modified: Sun, 09 Feb 2014 08:39:51 GMT
    ETag: "41879c-59-2a9c23c0"
    Accept-Ranges: bytes
    Content-Length: 89
    Content-Type: text/html; charset=UTF-8
    Set-Cookie: BIGipServerlpqwa1234-25046=1707657416.20480.0000; path=/
    
    [root@client3 ~] curl -I http://100.100.100.41/?app=qwa5678
    HTTP/1.1 200 OK
    Date: Tue, 05 Aug 2014 10:39:22 GMT
    Server: Apache/2.2.3 (CentOS)
    Last-Modified: Tue, 08 Nov 2011 12:26:29 GMT
    ETag: "4183f1-30-47e02740"
    Accept-Ranges: bytes
    Content-Length: 48
    Set-Cookie: AUTHTOKEN=5678; path=/
    Content-Type: text/html; charset=UTF-8
    Set-Cookie: BIGipServerlpqwa5678-25046=1724434632.20480.0000; path=/
    
    [root@client3 ~] curl -I http://100.100.100.41 -H "Cookie: BIGipServerlpqwa5678-25046=1724434632.20480.0000"
    HTTP/1.1 200 OK
    Date: Tue, 05 Aug 2014 10:39:39 GMT
    Server: Apache/2.2.3 (CentOS)
    Last-Modified: Tue, 08 Nov 2011 12:26:29 GMT
    ETag: "4183f1-30-47e02740"
    Accept-Ranges: bytes
    Content-Length: 48
    Set-Cookie: AUTHTOKEN=5678; path=/
    Content-Type: text/html; charset=UTF-8
    
     /var/log/ltm
    
    [root@B6900-R69-S40:Active:Standalone] config  tail -f /var/log/ltm
    Aug  5 03:54:15 B6900-R69-S40 info tmm3[15766]: Rule /Common/qux : client 100.100.100.3:32919 server 200.200.200.101:80  pool /Common/app-E2-openhome-25046
    Aug  5 03:54:22 B6900-R69-S40 info tmm1[15765]: Rule /Common/qux : client 100.100.100.3:32920 server 200.200.200.101:80  pool /Common/lpqwa1234-25046
    Aug  5 03:54:29 B6900-R69-S40 info tmm[15765]: Rule /Common/qux : client 100.100.100.3:32921 server 200.200.200.102:80  pool /Common/lpqwa5678-25046
    Aug  5 03:54:46 B6900-R69-S40 info tmm3[15766]: Rule /Common/qux : client 100.100.100.3:32922 server 200.200.200.102:80  pool /Common/lpqwa5678-25046