Kong:客户端关闭保持连接

Kong: Client Closing keep-alive connections

我正在为 kong 编写自定义插件。该插件将根据我的服务器 t运行sform request/response。我得到 [info] 27#0: *588 client <x> closed keepalive connection.

经过一番调试,发现每当设置 ngx.arg[1] 与我的 t运行 形成的反应。我已经关注了kong提供的现有response-transformer插件。

这是 kong body_filter 函数的主体:

local ctx = ngx.ctx
  local chunk, eof = ngx.arg[1], ngx.arg[2]

  ctx.rt_body_chunks = ctx.rt_body_chunks or {}
  ctx.rt_body_chunk_number = ctx.rt_body_chunk_number or 1

  if eof then
    local someChunks = concat(ctx.rt_body_chunks)
    local aBody = responseTransformer.transform(theConf, someChunks)
    aBody = unEscapeString(aBody)
    ngx.arg[1] = aBody or someChunks
  else
    ctx.rt_body_chunks[ctx.rt_body_chunk_number] = chunk
    ctx.rt_body_chunk_number = ctx.rt_body_chunk_number + 1
    ngx.arg[1] = nil
  end

我运行 与本地虚拟服务器相同的插件。它工作正常。但是当我代理到我的实际服务器时,我得到了 closed keepalive connection 错误。

从 kong 日志中,我可以看到响应运行格式正确。

使用 curl,我得到了大约一半的响应。

找到原因了。服务器正在发送 Content-Length header。在重写 body 期间,这保持不变。因此,在交付完整内容之前关闭了连接。

为了解决这个问题,我必须清除 header_filter 函数中的 Content-Length header:

kong.response.clear_header("Content-Length")