nginx lua 读取 content encoding 头信息

我正在尝试在 header_filter_by_lua 区块中读取 Content-Encoding。我使用 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"]

它们全部都返回空/nil值。以同样方式获取 User-Agent 是成功的,所以 Content-Encoding 的问题是什么?

点赞
用户2060502
用户2060502

ngx.var.http_content_encoding - 会返回请求头(不是响应头)

下面的 API 可以在 header_filter_by_lua_block 和后面的阶段中用于读取访问:

ngx.header.content_encoding 对我来说总是有效的,是正确的方式。

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

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

2018-05-22 11:40:40
用户1589811
用户1589811

获取请求的值使用以下代码:

ngx.req.get_headers()["content_encoding"]
2020-08-21 06:13:00