hi agentzh,
我在使用resty-redis时发现,它的错误信息试图用pcall截获时有问题,请问是我使用方法有错还是什么别的问题?
不包括pcall的配置文件如下,其中本地的 1111端口实际无服务,仅为测试出错。
server
{
server_name test;
access_log /data/logs/test.access.log;
error_log /data/logs/test.error.log warn;
default_type 'text/plain';
location /
{
content_by_lua '
local redis = require("resty.redis")
local red = redis:new()
red:set_timeout(500)
ok, err = red:connect("127.0.0.1", 1111)
ngx.say("1")
';
}
}
执行结果
[root@s conf]# curl "
http://test/"
1
[root@s conf]# tail -f /data/logs/test.error.log
2012/11/30 15:11:56 [error] 10516#0: *32 connect() failed (111: Connection refused), client: 127.0.0.1, server: test, request: "GET / HTTP/1.1", host: "test"
以上这段出错信息就是我想用 lua 截获的,使用程序来处理这个错误。
修改 content_by_lua 段为以下
content_by_lua '
local redis = require("resty.redis")
local red = redis:new()
red:set_timeout(500)
ok, err = pcall(red:connect, "127.0.0.1", 1111)
ngx.say(ok)
if not ok then ngx.log(ngx.CRIT, "cannot connect "..err) end
ngx.say("1")
';
[root@s conf]# curl "
http://test/"
<html>
<head><title>500 Internal Server Error</title></head>
<body bgcolor="white">
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx</center>
</body>
</html>
error.log
2012/11/30 15:29:44 [error] 10555#0: *41 failed to load Lua inlined code: [string "content_by_lua"]:5: function arguments expected near ',', client: 127.0.0.1, server: test, request: "GET / HTTP/1.1", host: "test"
这段看起来是写法不对
修改 content_by_lua 段如下
content_by_lua '
local redis = require("resty.redis")
local red = redis:new()
red:set_timeout(500)
ok, err = pcall(red.connect, "127.0.0.1", 1111)
ngx.say(ok)
if not ok then ngx.log(ngx.CRIT, "cannot connect "..err) end
ngx.say("1")
';
[root@s conf]# curl "
http://test/"
true
1
error.log 里没有任何内容!难道它连上了?我又特意看了一下,1111上确实什么都没有。
请问这个是什么情况?我应该怎么处理呢?
--
Lance