ADC

将 cookie 属性插入 ADC 生成的 cookie

网络管理员可以在 NetScaler 设备生成的 Cookie 中插入其他 cookie 属性。这些额外的 cookie 属性有助于根据应用程序访问模式为 ADC 生成的 Cookie 执行所需的策略。

以下功能使用 ADC 生成的 Cookie 来实现持久性。

  • 负载平衡缓存持久性
  • 负载平衡组 cookie 持久性
  • GSLB 网站持久化
  • 内容交换 cookie 持久性

您可以使用以下参数将其他 cookie 属性插入到 ADC 生成的 cookie 中:

  • LiteralADCCookieAttribute: 将其他 cookie 属性作为字符串追加到 ADC 生成的 cookie。

  • ComputedADCCookieAttribute: 使用 ADC ns 变量根据客户端或服务器属性(例如用户代理版本)有条件地将 cookie 属性附加到 ADC 生成的 cookie。

注意

您不能在负载平衡参数或单个负载平衡配置文件中同时配置文字 ADC Cookie 属性和计算的 ADC Cookie 属性。

每个 cookie 都有一个与之关联的域。当 Cookie 的域与用户地址栏中的网站域相匹配时,这被视为同站点(或第一方)上下文。如果与 Cookie 关联的域与外部服务相匹配,而不是与用户地址栏中的网站相匹配,则这被视为跨站点(或第三方)上下文。

SameSite 属性指示浏览器是否可以将 cookie 用于跨站点上下文或仅用于同站上下文。此外,如果应用程序打算在跨站点上下文中访问,那么它只能通过 HTTPS 连接进行访问。有关详细信息,请参阅 RFC6265

直到 2020 年 2 月,NetScaler 才明确设置了 SameSite 属性。浏览器将默认值设为“无”,并且没有影响 NetScaler 的部署。

但是,随着某些浏览器的升级,例如 Google Chrome 80,Cookie 的默认跨域行为发生了变化。可以将 SameSite 属性设置为以下值之一。Google Chrome 的默认值设置为 Lax。

  • 无: 表示浏览器仅在安全连接时才在跨站点上下文中使用 Cookie。
  • Lax: 表示浏览器在同一站点上下文中使用 cookie 处理请求。在跨网站上下文中,只有像 GET 请求这样的安全 HTTP 方法才能使用 cookie。
  • 严格: 仅在同一站点上下文中使用 cookie。

如果 Cookie 中没有 SameSite 属性,则 Google Chrome 会假定 SameSite=Lax 的功能。

注意

对于某些版本的其他浏览器,SameSite 属性的默认值可能设置为 None。在某些浏览器版本中,“SameSite = none”可以区别对待。例如,以下浏览器拒绝带有“SameSite = none”的 cookie:

  • Chrome 51 到 Chrome 66 的 Chrome 版本(包括两端)
  • Android 版本 12.13.2 之前的 UC 浏览器的版本

要配置 ADC 生成的 cookie 属性,必须执行以下操作:

  1. 创建负载平衡虚拟服务器
  2. 通过 LB 参数或 LB 配置文件为负载平衡虚拟服务器设置 ADC Cookie 属性。
  3. 如果您使用 LB 配置文件,请将 LB 配置文件设置为负载平衡虚拟服务器。
  4. 如果您选择使用计算的 ADC Cookie 属性,请配置相关的重写策略。

注意

如果 LB 配置文件绑定到 LB 虚拟服务器,则考虑配置文件参数配置,而不是全局 LB 参数配置。

您可以通过以下方法设置 ADC 生成的 cookie 属性:

  • 在负载平衡参数中设置 ADC cookie 属性
  • 在负载平衡配置文件中设置 ADC cookie 属性

要将策略统一应用于在 NetScaler 设备上配置的所有应用程序的 ADC 生成的 Cookie,可以在全局 LB 参数中设置 ADC cookie 属性。

文字 ADC Cookie 属性设置允许您无条件地将 cookie 属性插入 ADC 生成的 cookie。

在命令提示符下,键入:

set lb parameter -LiteralADCCookieAttribute <string>
<!--NeedCopy-->

示例:

set lb parameter -LiteralADCCookieAttribute SameSite=None
<!--NeedCopy-->

计算的 ADC Cookie 属性 设置允许您根据客户端或服务器属性有条件地将 cookie 属性插入 ADC 生成的 cookie 中。

在命令提示符下,键入:

set lb parameter -ComputedADCCookieAttribute <ns variable>
<!--NeedCopy-->

示例:

add ns variable cookieattribute_var -type "text(100)" -scope transaction
set lb parameter -ComputedADCCookieAttributE "$cookieattribute_var"
add ns assignment samesiteassign -variable "$cookieattribute_var" -set ""SameSite=None""

add policy expression pol_iphone "(HTTP.REQ.HEADER("User-Agent").CONTAINS("iP") && (HTTP.REQ.HEADER("User-Agent").REGEX_SELECT(re/OS \d+\\_/).REGEX_SELECT(re/\d+/).TYPECAST_NUM_T(DECIMAL).EQ(12).typecast_text_t ALT "false").eq("true"))"
add policy expression pol_chrome "(HTTP.REQ.HEADER("User-Agent").CONTAINS("Chrom") && (HTTP.REQ.HEADER("User-Agent").REGEX_SELECT(re/Chrom.*\d+./).REGEX_SELECT(re/\d+/).TYPECAST_NUM_T(DECIMAL).BETWEEN(51,66).typecast_text_t ALT "false").eq("true"))"
add rewrite policy exception_samesite_attribute "pol_iphone || pol_chrome " NOREWRITE
add rewrite policy append_samesite_attribute true samesiteassign

bind rewrite global exception_samesite_attribute 90 110 -type RES_OVERRIDE
bind rewrite global append_samesite_attribute 100 110 -type RES_OVERRIDE
<!--NeedCopy-->

使用 GUI 配置变量

  1. 导航到 AppExpert > 变量,然后单击 添加
  2. 在“创建变量”页面中,从下拉菜单中选择“范围事务”和“类型文本”。

    为 ADC Cookie 创建变量

  3. 输入其他详细信息,然后单击“创建”。

使用 GUI 创建任务

配置变量后,您可以赋值或通过创建赋值来指定要对该变量执行的操作。

  1. 导航到 AppExpert > 任务,然后单击添加
  2. 创建作业 页面中,输入详细信息,然后单击 创建
  1. 导航到流量管理 > 负载平衡 > 更改负载平衡参数

    更改 lb 参数

  2. 配置负载平衡参数 窗格中,根据您的要求为其中一个字段输入相应的值:

    • 字面意思 ADC Cookie 属性
    • 计算的 ADC Cookie 属性

    配置 lb parameters.png

  3. 单击确定

要为在 NetScaler 设备上配置的特定应用程序应用策略,可以在绑定到特定应用程序的 LB 虚拟服务器的 LB 配置文件中设置 cookie 属性参数。

LB 配置 文件中的“文字 ADC Cookie 属 性”设置允许您无条件地将 cookie 属性插入 ADC 生成的特定于虚拟服务器的 cookie。

在命令提示符下,键入:

add lb profile <profile name> -LiteralADCCookieAttribute <string>
<!--NeedCopy-->

示例:

add lb profile LB-Vserver-Profile-1 -LiteralADCCookieAttribute SameSite=None
add lb vserver LB-VServer-1 SSL 10.102.148.37 443 -persistenceType COOKIEINSERT -lbprofilename LB-Vserver-Profile-1
<!--NeedCopy-->

LB 配置文件中的 计算式 ADC Cookie 属性 设置允许您根据客户端或服务器属性有条件地将 cookie 属性插入 ADC 生成的 cookie 中。然后,将此 LB 配置文件设置为 LB 虚拟服务器。

在命令提示符下,键入:

add lb profile <profile name> -ComputedADCCookieAttribute <ns variable>
<!--NeedCopy-->

示例:

add ns variable cookieattribute_var -type "text(100)" -scope transaction
add ns assignment samesiteassign -variable "$cookieattribute_var" -set ""SameSite=None""
add lb profile LB-Vserver-Profile-1 -ComputedADCCookieAttributE "$cookieattribute_var"

add policy expression pol_iphone "(HTTP.REQ.HEADER("User-Agent").CONTAINS("iP") && (HTTP.REQ.HEADER("User-Agent").REGEX_SELECT(re/OS \d+\\_/).REGEX_SELECT(re/\d+/).TYPECAST_NUM_T(DECIMAL).EQ(12).typecast_text_t ALT "false").eq("true"))"
add policy expression pol_chrome "(HTTP.REQ.HEADER("User-Agent").CONTAINS("Chrom") && (HTTP.REQ.HEADER("User-Agent").REGEX_SELECT(re/Chrom.*\d+./).REGEX_SELECT(re/\d+/).TYPECAST_NUM_T(DECIMAL).BETWEEN(51,66).typecast_text_t ALT "false").eq("true"))"
add rewrite policy exception_samesite_attribute "pol_iphone || pol_chrome " NOREWRITE
add rewrite policy append_samesite_attribute true samesiteassign

add lb vserver LB-VServer-1 SSL 10.102.148.37 443 -persistenceType COOKIEINSERT -lbprofilename LB-Vserver-Profile-1
bind lb vserver LB-VServer-1 -policyName exception_samesite_attribute -priority 90 -gotoPriorityExpression 110 -type RESPONSE
bind lb vserver LB-VServer-1 -policyName append_samesite_attribute -priority 100 -gotoPriorityExpression 110 -type RESPONSE
<!--NeedCopy-->
  1. 导航到流量管理 > 负载平衡 > 虚拟服务器
  2. 选择虚拟服务器,然后单击 Edit(编辑)。
  3. 在“高级设置”部分下,单击“添加配置文件”。

    lb 虚拟服务器

  4. 在“配置文件”部分中,单击“添加”以创建 LB 配置文件。

    如果您已经创建了个人资料,请从 LB 配置文件 下拉菜单中选择它。

    添加资料

  5. 在“LB 配置文件”窗格中,根据您的要求为其中一个字段输入相应的值:
  • 字面意思 ADC Cookie 属性
  • 计算的 ADC Cookie 属性

    LB profile.png 中的字面计算属性

  1. 单击确定
  2. 将创建的 LB 配置文件设置为 步骤 1中创建的 LB 虚拟服务器。

验证 ns 变量配置

要验证在 LB 参数或 LB 配置文件中是否正确配置 ADC ns 变量,请使用 show lb 参数或 show lb 配置文件命令。

下表列出了 ns 变量配置不正确时的各种警告消息及其原因。

警告消息 原因
未配置 NS 变量。使用类型为 text () 和变量的作用域事务对其进行配置 尚未配置 NS 变量。
已配置的 NS 变量的作用域不是事务。 变量已配置,但范围未设置为“事务”。
变量的类型不是 Text ()。 变量已配置,但类型未设置为“文本”。
NS 变量配置的最大值大小大于 255。 为 NS 变量配置的值大于 255 个字符。注意: ADC 生成的 cookie 最多可以附加 255 个字符。超过最大长度的字符将被截断。

示例输出

在以下示例中,未配置 ns 变量时会显示警告消息。

set lb parameter -ComputedADCCookieAttribute "$lbvar"

Warning: NS Variable is not configured. Please configure it with type text() and scope transaction
Done
<!--NeedCopy-->

警告消息显示在该 show lb parameter 命令的以下输出中。

show lb parameter

Global LB parameters:
Persistence Cookie HttpOnly Flag: ENABLED
Use Encrypted Persistence Cookie: DISABLED
Use Port For Hash LB: YES
Prefer direct route: YES
Retain Service State: OFF
Start RR Factor: 0
Skip Maxclient for Monitoring: DISABLED
Monitor Connection Close: FIN
Use consolidated stats for LeastConnection: YES
Allow mac mode based vserver to pick thereturn traffic from services: DISABLED
Allow bound service removal: ENABLED
TTL for Domain Based Server: 0 secs

NetScaler Cookie Variable Name: $lbvar(NS Variable is not configured. Please configure it with type text() and scope transaction)

Done
<!--NeedCopy-->

以下示例配置适用于在与 LB 虚拟服务器对应的 GSLB 服务上配置的站点持久性。要将一些额外的 cookie 属性附加到 GSLB Cookie,请执行以下配置。

  • 在 LB 配置文件(LB-vserver-Profile-1)中设置 ADC cookie 属性。
  • 在 LB 配置文件中设置文字 ADC Cookie 属性值,例如“SameSite = None”。
  • 将 LB 配置文件设置为代表 GSLB 服务的负载平衡虚拟服务器 (LB-vServer-1)。
add gslb vserver GSLB-VServer-1 SSL -backupLBMethod ROUNDROBIN -tolerance 0 -appflowLog DISABLED
add gslb site site1 10.102.148.4 -publicIP 10.102.148.4
add gslb service site1_gsvc1 10.102.148.35 SSL 443 -publicIP 10.102.148.35 -publicPort 443 -maxClient 0 -siteName site1 -sitePersistence HTTPRedirect -sitePrefix ss1 -cltTimeout 180 -svrTimeout 360 -downStateFlush ENABLED

bind gslb vserver GSLB-VServer-1 -serviceName site1_gsvc1
bind gslb vserver GSLB-VServer-1 -domainName www.gslb.com -TTL 5

add service service-1 10.102.84.140 SSL 443

add lb profile LB-Vserver-Profile-1 -LiteralADCCookieAttribute SameSite=None
add lb vserver LB-VServer-1 SSL 10.102.148.37 443 -persistenceType COOKIEINSERT -lbprofilename LB-Vserver-Profile-1

bind lb vserver LB-VServer-1 service-1
<!--NeedCopy-->

注意

您也可以使用计算的 ADC Cookie 属性有条件地插入 cookie 属性。

以下示例配置适用于在内容交换虚拟服务器后托管多个应用程序的情况。要将相同的策略应用于所有应用程序,请将重写策略绑定到内容交换虚拟服务器而不是 LB 虚拟服务器,如下所示:

  • 在 LB 参数中设置 ADC Cookie 的属性。

    注意:

    您也可以在负载均衡配置文件中设置 ADC cookie 属性。

  • 将类型设置为文本的 ns 变量 (cookieattribute_var) 配置为文本并将范围设置为事务。
  • 使用 ns 变量在全局 LB 参数中设置计算的 ADC Cookie 属性。
  • 为内容交换虚拟服务器设置重写策略(exception_samesite_attribute 和 append_samesite_attribute),用于插入 cookie 属性。
add ns variable cookieattribute_var -type "text(100)" -scope transaction
set lb parameter -ComputedADCCookieAttributE "$cookieattribute_var"
add ns assignment samesiteassign -variable "$cookieattribute_var" -set ""SameSite=None""

add policy expression pol_iphone "(HTTP.REQ.HEADER("User-Agent").CONTAINS("iP") && (HTTP.REQ.HEADER("User-Agent").REGEX_SELECT(re/OS \d+\\_/).REGEX_SELECT(re/\d+/).TYPECAST_NUM_T(DECIMAL).EQ(12).typecast_text_t ALT "false").eq("true"))"
add policy expression pol_chrome "(HTTP.REQ.HEADER("User-Agent").CONTAINS("Chrom") && (HTTP.REQ.HEADER("User-Agent").REGEX_SELECT(re/Chrom.*\d+./).REGEX_SELECT(re/\d+/).TYPECAST_NUM_T(DECIMAL).BETWEEN(51,66).typecast_text_t ALT "false").eq("true"))"
add rewrite policy exception_samesite_attribute "pol_iphone || pol_chrome " NOREWRITE
add rewrite policy append_samesite_attribute true samesiteassign

add lb vserver LB-VServer-1 SSL 10.102.148.35 443
add lb vserver LB-VServer-2 SSL 10.102.148.36 443

add cs vserver CS-VServer-1 SSL 10.102.148.42 443 -persistenceType COOKIEINSERT

add cs action act1 -targetLBVserver v1
add cs action act2 -targetLBVserver v2
add cs policy CS-policy-1 -rule "HTTP.REQ.URL.CONTAINS("file1.html")" -action act1
add cs policy CS-policy-2 -rule "HTTP.REQ.URL.CONTAINS("file2.html")" -action act2

bind cs vserver CS-VServer-1 -policyName CS-policy-1 -priority 1
bind cs vserver CS-VServer-1 -policyName CS-policy-2 -priority 2

bind cs vserver -policyname exception_samesite_attribute 90 110 -type RES_OVERRIDE
bind cs vserver -policyname append_samesite_attribute 100 110 -type RES_OVERRIDE
<!--NeedCopy-->
将 cookie 属性插入 ADC 生成的 cookie