手边有个需求, 希望nginx直接把日志发送到elasticsearch内. 在github上找到了一个解决方法, https://github.com/bdeak/nginx-log-elasticsearch-lua ,但是跑不起来.
所以打算自己写一个简单的实现, 使用的resty.http .在log_by_lua_file 使用,提示API disabled in the current context. 网上找了下解决方法,说是通过ngx.timer.at的方式来搞定.
教程里例子给的很简单. 照抄回来以后,发现还是提示API disabled in the current context....
--------------------------------------------
local json = require "cjson"
local http = require "resty.http"
local httpc = http.new()
httpc:set_timeout(500)
httpc:connect("192.168.0.100",9200)
local ok,err = httpc:request {
path = "/" .. esindex .. "/" .. estype,
method = "POST",
-- body = json.encode(logdata),
body = logdata,
}
if not ok then
ngx.log(ngx.ERR,"ES error:",err)
end
end
local ok, err = ngx.timer.at(0, sendlog)
if not ok then
ngx.log(ngx.ERR,"ES err:",err)
return
end
-----------------------------
求大神解救...谢谢