Resolve hostname which is written in local hosts definition file (e.g. /etc/hosts
).
This library will be useful for using openresty
, this solves some issue about resolving hosts related problems.
resty.redis
couldn't resolvelocalhost
to127.0.0.1
-- issue- The
host
argument onngx.balancer.set_current_peer()
couldn't accept hostname, only ip address -- issue - When we use
docker
with linking other containers, we want to resolve linked host via/etc/hosts
To solve above things, parse host definition file and resolve hostname to local ip address.
-- Add package and require library
package.path = package.path .. ";/path/to/project/lua-local-resolver/?.lua"
local resolver = require "local-resolver"
-- Instantiate with host definition file path
local r = resolver.new("/etc/hosts")
-- Resolve hostname
print(r:resolve("localhost")) -- 127.0.0.1
Intialize at init_by_lua
directive:
init_by_lua_block {
local resolver = require "local-resolver"
-- expose global as we expect
resolver = resolver.new("/etc/hosts")
}
and use it following directives.
Balancer example:
balancer_by_lua_block {
local balancer = require "ngx.balancer"
-- resolve localhost to ip
local ok, err = balancer.set_current_peer(resolver:resolve("localhost"), 80)
...
-- Or, use linked docker container name
local ok, err = balancer.set_current_peer(resolver:resolve("docker-linked-name"), 80)
...
}
Redis example:
content_by_lua_block {
local redis = require "resty.redis"
local r = redis:new()
-- connect with hostname
local ok, err = r:connect(resolver:resolve("localhost"), 6379)
...
}
No longer you don't need to define environment variable like REDIS_HOST
at nginx.conf
:)
Of course this library only
resolves local definition hosts.
So, if you want to resolve external hosts, we prefer to use lua-resty-dns, it is bundled in openresty
.
Yoshiaki Sugimoto
MIT