這個(gè) Lua 庫(kù)提供了 ngx_lua 模塊的 DNS 解析器:
這個(gè) Lua 庫(kù)基于 ngx_lua 的 cosocket API 實(shí)現(xiàn),可以確定是 100% 非阻塞的。注意,該模塊至少需要 ngx_lua 0.5.12 或 OpenResty 1.2.1.11 版本。Lua bit 模塊也是需要的。如果你的 ngx_lua 綁定的 LuaJIT 的版本是 2.0,就已經(jīng)默認(rèn)開啟了 Lua bit 模塊。注意,這個(gè)模塊在 OpenResty 集成環(huán)境中默認(rèn)是開啟的。
使用代碼示例:
lua_package_path "/path/to/lua-resty-dns/lib/?.lua;;";
server {
location = /dns {
content_by_lua_block {
local resolver = require "resty.dns.resolver"
local r, err = resolver:new{
nameservers = {"8.8.8.8", {"8.8.4.4", 53} },
retrans = 5, -- 5 retransmissions on receive timeout
timeout = 2000, -- 2 sec
}
if not r then
ngx.say("failed to instantiate the resolver: ", err)
return
end
local answers, err = r:query("www.google.com")
if not answers then
ngx.say("failed to query the DNS server: ", err)
return
end
if answers.errcode then
ngx.say("server returned error code: ", answers.errcode,
": ", answers.errstr)
end
for i, ans in ipairs(answers) do
ngx.say(ans.name, " ", ans.address or ans.cname,
" type:", ans.type, " class:", ans.class,
" ttl:", ans.ttl)
end
}
}
}