nginx 配置间接监控 udp 负载均衡健康检查
1.nginx 搭建
2.nginx.conf 文件的配置
user root; worker_processes 1; error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { server { listen 80; # status interface location /status { healthcheck_status html; } # http front location / { proxy_pass http://http-cluster; } } # as a backend server. server { listen 8080; location / { root html; } } upstream http-cluster { # simple round-robin server 127.0.0.1:8080; server 127.0.0.2:81; check interval=3000 rise=2 fall=5 timeout=5000 type=http; check_http_send "GET / HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx; } } stream { upstream tcp-cluster { #simple round-robin server 127.0.0.1:22; server 192.168.0.2:22; server 192.168.221.141:12346; check interval=3000 rise=2 fall=5 timeout=5000 default_down=true type=tcp; } server { listen 5141; proxy_pass tcp-cluster; } upstream udp-cluster { # simple round-robin #server 127.0.0.1:53; #server 8.8.8.8:53; #server 192.168.221.141:5555; server 192.168.221.141:12345 weight=1; server 192.168.221.141:12346 weight=1; server 192.168.221.141:12347 weight=1; server 192.168.221.141:12348 weight=1; server 192.168.221.141:12349 weight=1; check interval=3000 rise=2 fall=5 timeout=5000 default_down=true type=udp; } server { listen 5140 udp; proxy_pass udp-cluster; } }
这里包含了 udp 和 tcp 的负载均衡,虽然实现了udp的负载均衡,但是在直接查看 udp 的健康情况时,udp 永远是健康的。这里采用方法是:被监控的 udp 服务同时监听 udp 和 tcp,在 nginx 这边查看健康情况时,若与 udp 同一 url 的 tcp 是健康的,则视其 udp 是健康的。 所以在配置 udp-cluster 的同时也要 配置到 tcp-cluster 中。