ngx.status 变量未被设置。

在我的 nginx 配置文件中,我设置了以下行以从 lua 中提供回退错误页面:

error_page 502 @fallback;

location @fallback {
     content_by_lua_file 'fallback.lua';
}

location / {
    return 502;
}

然后,在我的 lua 文件中,我在文件顶部写了以下内容:

ngx.log(ngx.ERR, "reported status is: " .. ngx.status)

我期望它是 502,但这报告 ngx.status 是 0。

我尝试通过编写以下内容来解决此问题

set $status 502

但 nginx 抱怨 $status 是现有变量的副本,不会加载配置。

如何让 lua 知道从 return 指令中获得的 nginx 状态?

点赞
用户329700
用户329700

看起来 Lua 模块中存在一个 bug,这导致设置无法正确进行。

https://github.com/chaoslawful/lua-nginx-module/commit/82ba941d

2013-10-23 00:12:52
用户2789404
用户2789404

这是 ngx_lua 中的一个错误。return指令的响应状态码与普通响应状态码存储方式不同,在 r->err_status 而不是 r->headers_out.status 中。ngx.status API 仅读取后者而非前者。

此问题已在 ngx_lua 的主分支中作为提交 82ba941d 进行修复:https://github.com/chaoslawful/lua-nginx-module/commit/82ba941d

此修复将包含在 ngx_lua(0.9.1)和 ngx_openresty(1.4.3.1)的下一个版本中。

感谢您的报告!

2013-10-23 00:14:56