ADC

将 cookie 属性插入 ADC 生成的 cookie

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

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

  • 负载平衡 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 属性指示浏览器是否可以用于跨站点上下文还是仅用于同一站点上下文。此外,如果应用程序打算在跨站点上下文中访问,那么它只能通过 HTTPS 连接进行访问。有关详细信息,请参阅 RFC6265

直到 2020 年 2 月, SameSite 属性没有在 Citrix ADC 中明确设置。浏览器将默认值设为“无”,并且不影响 Citrix ADC 部署。

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

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

如果 Cookie 中没有相同列表属性,Google Chrome 采用 SameSite = Lax 的功能。

注意

对于某些版本的其他浏览器,sameSite 属性的默认值可能设置为 。在某些浏览器版本中,“SameSite = 无”可以区别对待。例如,以下浏览器拒绝一个具有“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 属性

要统一对 Citrix ADC 设备上配置的所有应用程序的 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. 创建作业 页面中,输入详细信息,然后单击 创建

    为 ADC 变量创建赋值

  1. 导航到流量管理 > 负载平衡 > 更改负载平衡参数

    更改 lb 参数

  2. 在“配置负载平衡参数”窗格中,根据您的要求为以下任一字段输入相应的值:

    • 文字 ADC Cookie 属性
    • 计算的 ADC Cookie 属性

    configure lb parameters.png

  3. 单击 OK(确定)。

要为 Citrix ADC 设备上配置的特定应用程序应用策略,可以在绑定到特定于应用程序的 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. 单击 OK(确定)。
  2. 将创建的 LB 配置文件设置为在步骤 1 中创建的 LB 虚拟服务器。

验证 ns 变量配置

要验证 ADC ns 变量是否在 LB 参数或 LB 配置文件中正确配置,请使用 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

Citrix ADC 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 配置文件-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 属性。

    注意 : 您也可以在 LB 配置文件中设置 ADC cookie 属性。

  • 将类型的 ns 变量(cookie 属性 _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