Hello!
2013/5/9 martin qin:
> upstream backend {
> server 127.0.0.1:6379;
> # a pool with at most 1024 connections
> # and do not distinguish the servers:
> keepalive 1024;
> }
>
> location /redis_exists {
> internal;
> set_unescape_uri $key $arg_key;
> redis2_query exists $key;
> redis2_pass backend;
> }
>
> local res = ngx.location.capture("/redis_exists",
> { args = { key = key } })
>
> 上面这句会报出upstream connection timeout
>
我使用你的代码和配置在 Linux x86_64 系统上通过 nginx 1.2.8 + ngx_redis2 v0.10 运行,访问本地的
redis 服务器(版本 2.4.10),无法复现你说的“upstream connection timeout”错误。即使使用 ab
-c10 -n100000 -k 压测,也没有一条超时错误。
请确认:
1. 你使用的 nginx 核心和相关模块是足够新的版本;
2. 你的网络链路上没有问题;
3. 你在 nginx.conf 中的超时配置没有过小;
4. 你的 redis 服务器没有过载;
5. 你没有耗尽你系统的各项资源(特别是 CPU 资源)。
> local res = ngx.location.capture("/redis_get",
> { args = { key = key } } )
>
> if res.status ~= 200 or not res.body then
> ngx.log(ngx.ERR, "bad redis response:", res.body)
> ngx.exit(500)
> end
>
> local parser = require "redis.parser"
> local body, typ = parser.parse_reply(res.body)
>
> 1. key在redis中不存在时, body是nil
> 2. key在redis中存在, value是"", 此时body也是nil
> 我需要区分以上2种情况
>
你使用的是什么版本的 lua-redis-parser 库?在我这里测试 lua-resty-parser v0.10 是可以区分二者的,看下面这个例子:
location = /t {
content_by_lua '
local res = ngx.location.capture("/redis_get",
{ args = { key = "foo" } })
local parser = require "redis.parser"
local body, typ = parser.parse_reply(res.body)
if body == nil then
ngx.say("body is nil")
elseif body == "" then
ngx.say("body is empty string")
else
ngx.say("body is not empty: ", body)
end
';
}
我们先通过 redis-cli 命令行工具清空 redis 里所有的数据:
$ redis-cli
redis 127.0.0.1:6379> flushall
OK
然后访问 nginx 的 /t 接口得到:
$ curl localhost:8080/t
body is nil
然后我们再在 redis-cli 提示符上给 foo 这个键赋空字符串的值:
redis 127.0.0.1:6379> set foo ""
OK
此时再访问 /t 接口便得到
$ curl localhost:8080/t
body is empty string
不知你在你本地究竟是如何测试的?
另外,值得一提的是,推荐直接使用新的 lua-resty-redis 库来替换 ngx_redis2 + lua-redis-parser 这种组合:
https://github.com/agentzh/lua-resty-redis
这个库使用起来更加简便灵活,同时效率也会更高 :)
同时抄送给 openresty 中文邮件列表:https://groups.google.com/group/openresty
希望我们总是在那里交流这样的问题细节。谢谢合作 :)
Best regards,
-agentzh