注意:实际工程应该会用docker部署。
安装nginx
bash
sudo apt install libpcre3-dev zlib1g openssl -y
wget https://nginx.org/download/nginx-1.26.0.tar.gz
#安装到/home/gyl/workspace/mprpc/vendor/nginx-1.26.0下
tar xfzv nginx-1.26.0.tar.gz && cd nginx-1.26.0 && ./configure --with-stream --prefix=/home/gyl/workspace/mprpc/vendor/nginx-1.26.0
make && make install
vim nginx-1.26.0/conf/nginx.conf
追加以下内容:
stream {
upstream Server1 {
#后续在这里加配服务器即可增加集群
server 127.0.0.1:6001 weight=1 max_fails=3 fail_timeout=30s; #weight权重(涉及负载均衡策略),max_fails心跳次数
server 127.0.0.1:6002 weight=1 max_fails=3 fail_timeout=30s;
}
server {
proxy_connect_timeout 1s;
listen 8001; #nginx监听8001端口,客户端只需要连服务器的8001端口即可。
proxy_pass Server2;
tcp_nodelay on;
}
upstream Server2 {
server 127.0.0.1:6003 weight=1 max_fails=3 fail_timeout=30s; #weight权重(涉及负载均衡策略),max_fails心跳次数
server 127.0.0.1:6001 weight=1 max_fails=3 fail_timeout=30s;
}
server {
proxy_connect_timeout 1s;
listen 8000; #nginx监听8000端口,客户端只需要连服务器的8000端口即可。
proxy_pass Server2;
tcp_nodelay on;
}
}
上面的配置使nginx监听8001和8002两个端口,访问8001会转发给6001和6002,访问8002会转发给6003和6004,至此,实现2个集群。
使用
bash
cd nginx-1.26.0/sbin
sudo ./nginx #启动
sudo ./nginx -s stop #停止
sudo ./nginx -s reload #修改配置后平滑重启