You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Deno的WebSocket连接中设置Cookie实现Nginx Ingress会话亲和?

在Deno无服务器函数中为WebSocket连接设置Cookie以配合Nginx Ingress会话亲和

核心结论

可以在Deno的WebSocket连接中传递Cookie,只需在初始化连接时通过请求头手动配置即可。

解决方案步骤

1. 直接为WebSocket连接添加Cookie请求头

无论是使用Deno原生WebSocket还是第三方StandardWebSocketClient,都支持通过headers选项传递Cookie:

如果使用Deno原生WebSocket:

const wsUrl = `wss://${this.username}:${this.password}@${this.host}/api/${this.id}`;
const headers = new Headers();
// 替换为实际的亲和Cookie值,或按下方方法先从Ingress获取
headers.set('Cookie', 'cookie-affinity=YOUR_AFFINITY_TOKEN');

this._ws = new WebSocket(wsUrl, { headers });
this.logger.info(`Connecting to WebSocket URL with affinity cookie: ${wsUrl}`);

如果使用StandardWebSocketClient第三方库:

const wsUrl = `wss://${this.username}:${this.password}@${this.host}/api/${this.id}`;
const headers = new Headers();
headers.set('Cookie', 'cookie-affinity=YOUR_AFFINITY_TOKEN');

this._ws = new StandardWebSocketClient(wsUrl, { headers });
this.logger.info(`Connecting to WebSocket URL with affinity cookie: ${wsUrl}`);

2. 从Nginx Ingress获取并复用亲和Cookie

由于你的Ingress配置了affinity: cookie,该Cookie是由Ingress主动生成并返回给客户端的。因此更严谨的流程是:先发起一个普通HTTP请求到Ingress,获取它设置的cookie-affinity,再将该Cookie用于WebSocket连接:

async function connectWebSocketWithAffinity() {
  const httpUrl = `https://${this.host}/api/health`; // 选择一个能触发Ingress响应的接口
  const authToken = btoa(`${this.username}:${this.password}`);

  // 第一步:发起HTTP请求获取Ingress设置的亲和Cookie
  const response = await fetch(httpUrl, {
    headers: {
      'Authorization': `Basic ${authToken}`
    }
  });

  const cookieHeader = response.headers.get('set-cookie');
  if (!cookieHeader) {
    this.logger.warn('No affinity cookie received from Ingress');
    // 无Cookie时 fallback 到普通连接
    this._ws = new StandardWebSocketClient(wsUrl);
    return;
  }

  // 解析出cookie-affinity的值
  const affinityCookie = cookieHeader.split(';')
    .find(c => c.trim().startsWith('cookie-affinity='));
  
  if (!affinityCookie) {
    this.logger.warn('Affinity cookie not found in response');
    this._ws = new StandardWebSocketClient(wsUrl);
    return;
  }

  const cookieValue = affinityCookie.split('=')[1];
  const wsUrl = `wss://${this.username}:${this.password}@${this.host}/api/${this.id}`;
  
  // 第二步:使用获取到的Cookie建立WebSocket连接
  const headers = new Headers();
  headers.set('Cookie', `cookie-affinity=${cookieValue}`);
  this._ws = new StandardWebSocketClient(wsUrl, { headers });
  this.logger.info(`Connected with affinity cookie: cookie-affinity=${cookieValue}`);
}

// 调用函数执行连接
connectWebSocketWithAffinity();

之前方案失效的原因

  • URL参数方案:Nginx Ingress的cookie亲和模式默认仅识别Cookie中的令牌,无内置支持从URL参数提取亲和标识,因此该方案不可行。
  • 依赖浏览器自动设置:Deno无服务器函数运行在服务端环境,没有浏览器的自动Cookie管理机制,所有请求头都需要手动配置。

内容的提问来源于stack exchange,提问作者Alita

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 11:05:11