Ubuntu / Raspberry Pi – Install NGINX Load Balance DNS

| | | | | | | |
sudo apt install nginx

Replace the IP addresses with your IP addresses

sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf-original

sudo sed -i "/^\s*#/d;s/\s*#[^\"']*$//" /etc/nginx/nginx.conf && sudo sed -i '/^\s*$/d' /etc/nginx/nginx.conf

Modify your config file to look like this

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
	worker_connections 768;
}
http {
	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	include /etc/nginx/mime.types;
	default_type application/octet-stream;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_prefer_server_ciphers on;
	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;
	gzip on;
	gzip_disable "msie6";
	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}
stream{
	server {
		listen 53  udp;
		listen 53; #tcp
		proxy_pass      dns_servers;
		error_log       /var/log/nginx/dns.log info;
		proxy_responses 1;
		proxy_timeout   1s;
	}

	upstream dns_servers {
		zone dns_mem 64k;
		server 10.40.20.6:53 fail_timeout=10s;
		server 10.40.20.5:53 fail_timeout=10s;
	}
}

When done you need to test the config

nginx -t

You should see something like

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Reference: https://www.nginx.com/blog/load-balancing-dns-traffic-nginx-plus/

All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.