原理实际行就是stream模式,因为nginx 支持基于stream 模式的lb同时支持(tcp.udp)以下是一个简单的配置
环境准备
docker-compose 文件
version: "3"
services:
lb:
image: openresty/openresty:alpine
volumes:
- "./nginx-lb.conf:/usr/local/openresty/nginx/conf/nginx.conf"
- "./dns.log:/var/log/nginx/dns.log"
ports:
- "53:53/udp"
- "53:53/tcp"
- nginx 配置
worker_processes 1;
user root;
events {
worker_connections 1024;
}
stream {
upstream dns_servers {
server 114.114.114.114:53;
server 8.8.8.8:53;
server 8.8.8.4:53;
}
server {
listen 53 udp;
listen 53; #tcp
proxy_pass dns_servers;
error_log /var/log/nginx/dns.log info;
}
}
- 启动