
だいぶ前から、最近は採用がとても多いproxyソフトウェアであるNginxでUDPのproxyが出来るようになっていました。
また公式にも一応DNSのロードバランスについて記事があったので、自分の環境でも試してみました。

TCP and UDP Load Balancing | NGINX Plus
This chapter describes how to use NGINX Plus and NGINX Open Source to proxy and load balance TCP and UDP traffic.
ぶっちゃけ公式の通りにやるだけです。
環境:
$ nginx -v
nginx version: nginx/1.15.7
手順
必要最小限プラスアルファで以下のようにnginx.conf
を用意するだけです。
stream {
upstream dns_servers {
server 1.1.1.1:53;
server 8.8.8.8:53;
}
server {
listen 53 udp;
listen 53; #tcp
proxy_pass dns_servers;
error_log /var/log/nginx/dns.log info;
}
Nginx起動前に一応以下のコマンドを実行して、コンフィグに問題がないことを確認しておきます。
$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
さらに・・・
で、ここからが自分にとっての本題なのですが、問い合わせ先に応じて、バックエンドに問い合わせる向きを変えたいのですが、Nginxでudpの場合はよく分からず(問い合わせるドメイン別に、問い合わせるDNSサーバを振り分けたい、ということです)。
公式のドキュメントを読んだ限り、mapモジュールを使用すれば実現できるのかと思ったのですが、用意されている組み込み変数には、問い合わせ先の情報を持っている変数はなさそうなんですよね・・・
Module ngx_stream_core_module
Module ngx_stream_map_module
少なくとも以下のように設定をした限りでは動作しませんでした。
stream {
upstream recursor_backends {
server 1.1.1.1:53;
server 8.8.8.8:53;
}
upstream auth_backends {
server 192.168.1.1:53;
}
map $hostname $backends {
.example.com "auth_backends";
192.168.1.* "auth_backends";
default "recursor_backends";
}
server {
listen 53 udp reuseport rcvbuf=4m sndbuf=4m;
listen 53 reuseport rcvbuf=4m sndbuf=4m;
allow 192.168.0.0/16;
allow 127.0.0.0/8;
allow 127.0.0.1;
deny all;
proxy_buffer_size 64k;
proxy_pass $backends;
proxy_responses 1;
proxy_timeout 1s;
}
Nginxスクリプト、もしくはnginxのlua-nginx-module
を使用することで実現できるんですかねぇ。このあたりの知見をお持ちの方、本当に教えてほしいです。
openresty/lua-nginx-module: Embed the Power of Lua into NGINX HTTP servers
※おとなしくdnsdistを使えばいいんでしょうけど・・・