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 中。