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 #修改配置后平滑重启
相关推荐
静水楼台x3 小时前
nginx日志的一点理解
运维·nginx
欧先生^_^4 小时前
ingress-nginx 开启 Prometheus 监控 + Grafana 查看指标
nginx·grafana·prometheus
眠修5 小时前
Nginx + Tomcat负载均衡群集
nginx·tomcat·负载均衡
瘦皮猴10 小时前
golang context canceled异常排查
后端·nginx
eternal__day10 小时前
Spring Cloud 多机部署与负载均衡实战详解
java·spring boot·后端·spring cloud·负载均衡
hunter12714 小时前
Nginx 事件驱动理解
nginx
fydw_7151 天前
生产环境中安装和配置 Nginx 以部署 Flask 应用的详细指南
运维·nginx·flask
xzh1 天前
问题:Nginx client_body_temp_path 文件会删除吗,删除时机?
nginx·架构
dessler1 天前
代理服务器-LVS的3种模式与调度算法
运维·服务器·网络·算法·nginx·tomcat·lvs
2501_911121231 天前
Nginx+Tomcat 负载均衡群集
nginx·tomcat·负载均衡