官方参考文档:http://nginx.org/en/docs/stream/ngx_stream_core_module.html
只有nginx1.9以上的版本才支持tcp负载均衡
配置必须出现在main段,不能配置在http,event和server标签段
(1)安装官方nginx
1.配置官方yum源
#vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=0
enabled=1
yum makecache
yum repolist
2.安装nginx
yum install nginx -y
systemctl start nginx
systemctl enable nginx
(2)配置tcp负载均衡
#vim /etc/nginx/nginx.conf
stream {
upstream ssh_proxy {
hash $remote_addr consistent; //一致性hash
server 192.9.191.31:22 max_fails=2 fail_timeout=2s; //健康状态检测
server 192.9.191.32:22 max_fails=2 fail_timeout=2s;
}
server {
listen 2222;
proxy_connect_timeout 1s; //连接超时
proxy_timeout 20s; //连接超时时间,如果不配置,永远不超时
proxy_pass ssh_proxy;
}
}
#systemctl reload nginx