This article describes how to configure rule-based persistency on a NetScaler appliance.
When rule-based persistence is configured, the appliance creates a persistent session based on the contents of the matched rule before directing the request to the service selected by the configured load balancing method. Subsequently, it directs all requests that match the rule to the same service.
Using rule-based persistency on a load balancing virtual server, along with two advanced expressions, allows the appliance to persist network traffic to a specific service, based for example on a session cookie. One of the expressions is used to capture the cookie value from the HTTP response and the other expression to make a persistence decision on the next HTTP request with the session cookie.
To configure the rule-based persistency on a NetScaler appliance, run the following commands:
add policy expression rule_based_persistence_request "HTTP.REQ.HEADER(\"cookie\").VALUE(0).TYPECAST_NVLIST_T(\'=\',\';\').VALUE(\"TestCookie\")"
add policy expression rule_based_persistence_response "HTTP.RES.HEADER(\"set-cookie\").VALUE(0).TYPECAST_NVLIST_T(\'=\',\';\').VALUE(\"TestCookie\")"
add lb vserver lb-vserver-http HTTP 10.12.42.170 80 -persistenceType RULE -lbMethod ROUNDROBIN -rule rule_based_persistence_request -resRule rule_based_persistence_response
add service red-service red-server HTTP 80
add service blue-service blue-server HTTP 80
bind lb vserver lb-vserver-http red-service
bind lb vserver lb-vserver-http blue-service
Rum the following command to verify if the persistency is configured accurately:
> show lb vserver <name>
lb-vserver-170-http (10.12.42.170:80) - HTTP Type: ADDRESS State: UP Last state change was at Sat Aug 25 05:49:45 2012 Time since last state change: 33 days, 05:43:07.840 Effective State: UP Client Idle Timeout: 180 sec Down state flush: ENABLED Disable Primary Vserver On Down : DISABLED Appflow logging: DISABLED Port Rewrite : DISABLED No. of Bound Services : 2 (Total) 2 (Active) Configured Method: ROUNDROBIN Rule: rule_based_persistence_request RespRule: rule_based_persistence_response Mode: IP Persistence: RULE Persistence Timeout: 2 min Vserver IP and Port insertion: OFF Push: DISABLED Push VServer: Push Multi Clients: NO Push Label Rule: none L2Conn: OFF IcmpResponse: PASSIVE 1) red-svc (10.12.59.213: 80) - HTTP State: UP Weight: 1 2) blue-svc (10.12.59.212: 80) - HTTP State: UP Weight: 1
To test this configuration, you can create a PHP file setcookie.php within the Web site root folder and then use the PHP function setcookie to set a test cookie on the HTTP response.The setcookie.php file contains the following content:
<?php $value = 'red-server'; setcookie("TestCookie", $value); ?>
The first HTTP request does not contain the session cookie. At this point the appliance makes a load balance decision.
http://lbvserver.hv.lab/setcookie.php
GET /setcookie.php HTTP/1.1 Host: lbvserver.hv.lab User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Connection: keep-alive Range: 5 test-header: testing
The script has set the cookie on the response. At this moment, the NetScaler appliance uses the red-server as the persistence parameter.
HTTP/1.1 200 OK Date: Thu, 27 Sep 2012 15:56:06 GMT Server: Apache/2.2.3 (CentOS) X-Powered-By: PHP/5.1.6 Set-Cookie: TestCookie=red-server Content-Length: 110 Connection: close Content-Type: text/html; charset=UTF-8
Second HTTP request contains the session cookie. At this point, the NetScaler appliance makes persistence decision.
http://lbvserver.hv.lab/setcookie.phpGET /setcookie.php HTTP/1.1 Host: lbvserver.hv.lab User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0.1) Gecko/20100101 Firefox/8.0.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip, deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Connection: keep-alive Cookie: TestCookie=red-server Range: 5 test-header: testing
The following command shows an entry on the persistence table and the persistence parameter being used:
> show persistentSessions
Type SRC-IP DST-IP PORT VSNAME TIMEOUT PERSISTENCE-PARAMETER RULE 0.0.0.0 10.12.59.213 80 lb-vserver-http 0 red-server Done