[[linux_wiki:dns_load_balancing]]

DNS Load Balancing

General Information

DNS load balancing with Nginx streams.

Checklist

  • Install Nginx

Main Config File

Replace the entire main config (/etc/nginx/nginx.conf) with:

## NGINX - Main Configuration ##
 
# Context: Main - General Server Configuration
 
# User that worker processes run as
user  nginx;
 
# Number of worker processes (auto = set to number of CPUs)
worker_processes  auto;
 
# Error logging and PID of main process
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
 
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
 
# Include enabled configurations
include /etc/nginx/conf.d/enabled/*.conf;
 
# Context: Events - Connection Processing
events {
  # Max number of connections per worker process
  worker_connections  1024;
}
 
# No http contexts because we are doing stream processing with the included drop in files

Nginx DNS Load Balance Stream Config

Stream config drop in file.

/etc/nginx/conf.d/available/dns_loadbalance.conf

stream {
 
    # Stream Logging Setup
    log_format proxy '"Src:$remote_addr" [$time_local] '
                 '"Prot:$protocol" "Status:$status" "Sent:$bytes_sent" "Recv:$bytes_received" '
                 '"SesTime:$session_time" "DNS:$upstream_addr" '
                 '"Sent:$upstream_bytes_sent" "Recv:$upstream_bytes_received" "ConnTime:$upstream_connect_time"';
 
    # Log Location
    access_log  /var/log/nginx/dns_access.log proxy;
 
    # Backend DNS Servers
    upstream dns_servers {
        # Balancing Type: Least Connections
        least_conn;
        # Passive Health Checks (defaults): fail_timeout=10, max_fails=1
        # (server marked down if 1 failure in 10 second period and stays down for 10 seconds)
        # weight=5 : use these dns servers 5 times more than others with no weight
        server 192.168.1.1:53 weight=5;
        server 192.168.1.2:53 weight=5;
        server 192.168.1.3:53;
    }
 
    # Frontend listener
    server {
        listen     53 udp;
        proxy_pass dns_servers;
        # Bind response to interface request was received on
        proxy_bind $server_addr;
        proxy_timeout 1s;
        proxy_responses 1;
        error_log /var/log/nginx/dns_errors.log;
    }
}

  • linux_wiki/dns_load_balancing.txt
  • Last modified: 2019/05/26 03:50
  • (external edit)