nginx配置tcp长连接实现集群

注意:实际工程应该会用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 #修改配置后平滑重启
相关推荐
☆凡尘清心☆13 小时前
CentOS Stream 9 专用 LNMP 全自动一键脚本
linux·运维·mysql·nginx·lnmp·centos stream 9
nvd1119 小时前
GCP L4 Passthrough 负载均衡器“假死超时”深度排查复盘
运维·php·负载均衡
nvd1120 小时前
GCP 4层 外部网络负载均衡器深度解析
运维·网络·负载均衡
陈聪.1 天前
HAProxy 知识整理:从负载均衡原理到实战配置
运维·负载均衡
似的8351 天前
一步一步学习使用FireMonkey动画() 使用TAnimator类创建动画
linux·学习·nginx
看昭奚恤哭1 天前
平滑加权轮询负载均衡的底层逻辑
运维·负载均衡
小张同学a.2 天前
LAMP架构2
linux·运维·网络·架构·负载均衡
闪亮的路灯2 天前
威联通QTS使用自带web服务期代理前端(类似nginx)
前端·nginx·威联通
難釋懷3 天前
Nginx外置缓存-error_page
运维·nginx·缓存
怪味趣多3 天前
r创建动画。 . 使用TFloatAnimation通过改变图片的位置创建动车。 . 使用TPathAnimation创建路径动画。 ...
运维·nginx