Nginx content_by_lua 404错误处理

正在使用 lua 脚本查找存储在 redis 中的数据 - 它提供了一些关于 etags 和 mime 类型的额外处理,防止使用基本的 redis 模块。

我遇到的问题是在传递 404 错误给 nginx.conf 进行错误处理时,尽管设置了状态等信息,但错误似乎没有被捕获。

在理想的情况下!在无法在 redis 中定位数据的情况下,以下内容将允许返回临时图像。

nginx.conf

location /prefix/ {
  content_by_lua_file conf/redis.lua;
  error_page 404 = /images/not-available.png;
}

redis.lua

...
local arr, error = red:hgetall(uri)
if not arr then
  ngx.log(ngx.ERR, "Redis failed locate uri: "..uri)
  ngx.status = 404
return ngx.exit(ngx.HTTP_NOT_FOUND)
end
...
点赞
用户1825052
用户1825052

问题已得到解决 - 最终不是404处理程序,而是if语句中的比较问题。

local arr, error = red:hgetall(uri)
if not arr then

即使出现错误,似乎也会将arr设置为初始值-与值nil进行比较解决了问题。

2013-10-10 14:50:48