Hello,
I've two dns that have differents purpose. Two of them listen locally and it's nginx which expose the udp port 53.
Basically one just filter some domain names and the other allow all the traffic.
I 'm choosing between the two dns based on a variable store in a redis database.
I would like to find a wat to script nginx in lua or else to read the redis variable and choose the right dns server to forward the request to.
I saw that I've to use stream directive to listen to the 53 udp port. But I cannot find a proper way to make the lua scripting working.
If you, nginx guru, have a solution to purpose I will be very thankfull.
Here's my no-working code:
stream {
upstream dns1 { server 172.16.0.1:53; }
upstream dns2 { server 172.16.0.2:53; }
server {
listen 53 udp;
# That part not working
set $dns;
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
local ok, err = red:connect("redis-ip", 6379)
if not ok then
nginx.say("failed to connect: ", err)
return
end
local res, err = red:get("The redis var")
if not res then
ngx.var.dns = upstream.get_servers("dns1")
else
ngx.var.dns = upstream.get_servers("dns2")
end
}
proxy_pass $dns
}
}
I've two dns that have differents purpose. Two of them listen locally and it's nginx which expose the udp port 53.
Basically one just filter some domain names and the other allow all the traffic.
I 'm choosing between the two dns based on a variable store in a redis database.
I would like to find a wat to script nginx in lua or else to read the redis variable and choose the right dns server to forward the request to.
I saw that I've to use stream directive to listen to the 53 udp port. But I cannot find a proper way to make the lua scripting working.
If you, nginx guru, have a solution to purpose I will be very thankfull.
Here's my no-working code:
stream {
upstream dns1 { server 172.16.0.1:53; }
upstream dns2 { server 172.16.0.2:53; }
server {
listen 53 udp;
# That part not working
set $dns;
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
local ok, err = red:connect("redis-ip", 6379)
if not ok then
nginx.say("failed to connect: ", err)
return
end
local res, err = red:get("The redis var")
if not res then
ngx.var.dns = upstream.get_servers("dns1")
else
ngx.var.dns = upstream.get_servers("dns2")
end
}
proxy_pass $dns
}
}