nginx lua读取内容编码头

adr*_*rhc 1 lua nginx http-headers

我正在尝试读取 header_filter_by_lua 块中的内容编码。我使用 chrome 的开发人员工具进行测试,同时请求一个以 Content-Encoding: gzip 响应的 url。我使用这些检查:

local test1 = ngx.var.http_content_encoding
local test2 = ngx.header.content_encoding
local test3 = ngx.resp.get_headers()["Content-Encoding"]
Run Code Online (Sandbox Code Playgroud)

它们都给出空/零值。同样的方式获取User-Agent是成功的,那么Content-Encoding有什么问题呢?

Ale*_*ler 7

ngx.var.http_content_encoding- 将返回请求的(而不是响应的)标头

下面的 API 适用于以下上下文中的读取访问header_filter_by_lua_block下面的 API 适用于在后续阶段

ngx.header.content_encoding总是对我有用并且是正确的方法。

如果它不起作用 - 检查https://github.com/openresty/lua-nginx-module#lua_transform_underscores_in_response_headers

ngx.resp.get_headers()["Content-Encoding"]也可以,但获取单个标头的效率不高。