原文:https://blog.csdn.net/bigtree_3721/article/details/72833955

nginx-1.9.0 已发布,该版本增加了 stream 模块用于一般的 TCP 代理和负载均衡。

The ngx_stream_core_module module is available since version 1.9.0. This module is not built by default, it should be enabled with the --with-stream configuration parameter.

ngx_stream_core_module 这个模块在1.90版本后将被启用。但是并不会默认安装,需要在编译时通过指定 --with-stream 参数来激活这个模块。 
其他改进包括: 
Change: 删除过时的 aio 和 rtsig 事件处理方法 
Feature: 可在 upstream 块中使用 "zone" 指令 
Feature: 流模块,支持 TCP 代理和负载均衡 
Feature: ngx_http_memcached_module 支持字节范围 
Feature: Windows 版本支持使用共享内存,带随机化地址空间布局. 
Feature: "error_log" 指令可在 mail 和 server 级别 
Bugfix: the "proxy_protocol" parameter of the "listen" directive did not work if not specified in the first "listen" directive for a listen socket.

编译安装:略
最后贴一下官方分享的stream模块的简单配置demo:
http://nginx.org/en/docs/stream/ngx_stream_core_module.html

worker_processes auto;

error_log /var/log/nginx/error.log info; 
events { 
    worker_connections 1024; 
}

stream { 
    upstream backend { 
        hash $remote_addr consistent; 
        server backend1.example.com:12345 weight=5; 
        server 127.0.0.1:12345 max_fails=3 fail_timeout=30s; 
        server unix:/tmp/backend3; 
    }

server { 
        listen 12345; 
        proxy_connect_timeout 1s; 
        proxy_timeout 3s; 
        proxy_pass backend; 
    }

server { 
        listen [::1]:12345; 
        proxy_pass unix:/tmp/stream.socket; 
    } 
}

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
在此我做了一个tcp反向解析的小实验

背景:125.208.14.177:3306    数据库1 
      125.208.14.177:3306    数据库2 
      218.78.186.162        nginx服务器

配置文件

worker_processes auto;

error_log /var/log/nginx/error.log info; 
events { 
    worker_connections 1024; 
}

stream { 
    upstream backend { 
        hash $remote_addr consistent; 
        server 125.208.14.177:3306 weight=5 max_fails=3 fail_timeout=30s; 
        server 125.208.14.177:3307 weight=4 max_fails=3 fail_timeout=30s; 
    }

server { 
        listen 12345; 
        proxy_connect_timeout 1s; 
        proxy_timeout 3s; 
        proxy_pass backend; 
    }

}

测试:

[root@iZ236mlq2naZ ~]# mysql -uroot -p'******' -P12345 -h218.78.186.162 -e "select * from test" test

Warning: Using a password on the command line interface can be insecure.
+-----------------------------+
| t                          |
+-----------------------------+
| this is 125.208.14.177:3306 |
+-----------------------------+
[root@iZ236mlq2naZ ~]# mysql -uroot -p'*****' -P12345 -h218.78.186.162 -e "select * from test" test
Warning: Using a password on the command line interface can be insecure.
^[[A+-----------------------------+
| t                          |
+-----------------------------+
| this is 125.208.14.177:3307 |
+-----------------------------+
[root@iZ236mlq2naZ ~]# mysql -uroot -p'*****' -P12345 -h218.78.186.162 -e "select * from test" test
Warning: Using a password on the command line interface can be insecure.
+-----------------------------+
| t                          |
+-----------------------------+
| this is 125.208.14.177:3306 |
+-----------------------------+
[root@iZ236mlq2naZ ~]# mysql -uroot -p'******' -P12345 -h218.78.186.162 -e "select * from test" test
Warning: Using a password on the command line interface can be insecure.
+-----------------------------+
| t                          |
+-----------------------------+
| this is 125.208.14.177:3306 |
+-----------------------------+

再做一个读写分离的实验:
配置文件

worker_processes auto;

error_log /var/log/nginx/error.log info; 
events { 
    worker_connections 1024; 
}

stream { 
    upstream readdb { 
        hash $remote_addr consistent;                                    ---作为read库 
        server 125.208.14.177:3306 weight=5 max_fails=3 fail_timeout=30s; 
        server 125.208.14.177:3307 weight=4 max_fails=3 fail_timeout=30s; 
    }

server { 
        listen 12345; 
        proxy_connect_timeout 1s; 
        proxy_timeout 3s; 
        proxy_pass readdb; 
    }

upstream writedb{ 
      hash $remote_addr consistent; 
      server 125.208.14.177:3308 max_fails=3 fail_timeout=30s;      ---作为write库 
    } 
    server { 
        listen 23456; 
        proxy_connect_timeout 1s; 
        proxy_timeout 3s; 
        proxy_pass writedb; 
    } 
 
}

注意下:绿色stream就是表示该nginx使用了tcp来负载均衡,而以前的是基于http的负载均衡。http负载均衡在配置文件中在绿色stream地方要用http来替换,见本博的另外一篇关于nginx http负载均衡的例子。

最后可以将http负载与tcp负载写一起达到多重目的。

Nginx基于TCP的负载均衡的配置例子的更多相关文章

  1. Nginx反向代理和负载均衡的配置

    1.反向代理配置 反向代理也称"动静分离",nginx不自己处理图片的相关请求,而是把图片的请求转发给其他服务器来处理. 修改nginx部署目录下conf子目录的nginx.con ...

  2. Nginx 反向代理与负载均衡的配置

    已经很久没有写博了,因为最近学车加上各种问题一直没时间, 今天刚好想起有好多的东西还没来得及记录.回到正题: Nginx是一个非常强大的web轻量级服务器,许多大厂也用Nginx进行负载均衡和反向代理 ...

  3. Nginx反向代理和负载均衡——个人配置

    #user nobody; worker_processes 2; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...

  4. Nginx基于TCP/UDP端口的四层负载均衡(stream模块)配置梳理

    通过我们会用Nginx的upstream做基于http/https端口的7层负载均衡,由于Nginx老版本不支持tcp协议,所以基于tcp/udp端口的四层负载均衡一般用LVS或Haproxy来做.至 ...

  5. 使用apache和nginx代理实现tomcat负载均衡及集群配置详解

    实验环境: 1.nginx的代理功能 nginx proxy: eth0: 192.168.8.48 vmnet2 eth1: 192.168.10.10 tomcat server1: vmnet2 ...

  6. 使用nginx sticky实现基于cookie的负载均衡

    在多台后台服务器的环境下,我们为了确保一个客户只和一台服务器通信,我们势必使用长连接.使用什么方式来实现这种连接呢,常见的有使用nginx自带的ip_hash来做,我想这绝对不是一个好的办法,如果前端 ...

  7. nginx负载均衡及配置

    nginx负载均衡及配置 1 负载均衡概述 负载均衡由来是因为当一台服务器单位时间内的访问量很大时,此时服务器的压力也会很大,当超过自身承受能力时,服务器就会崩溃.为避免让服务器崩溃,用户拥有更好的体 ...

  8. nginx如何做到TCP的负载均衡

    原文:https://blog.csdn.net/u011218159/article/details/50966861   TCP 的 负载均衡   这个片段描述了如何通过nginx plus进行负 ...

  9. 使用nginx sticky实现基于cookie的负载均衡【转】

    在多台后台服务器的环境下,我们为了确保一个客户只和一台服务器通信,我们势必使用长连接.使用什么方式来实现这种连接呢,常见的有使用nginx自带的ip_hash来做,我想这绝对不是一个好的办法,如果前端 ...

随机推荐

  1. Django 多表查询 聚合查询 分组查询 F查询 Q查询

    # -------------------------------------------------------------------------------------------------- ...

  2. 【日常记录】【unity3d】 OnTriggerEnter 和 OnCollisionEnter (2D) 的区别

    问题:两个物体A,B 两者都有碰撞体 collider(Box Collider,Sphere Collider,Capsule Collider等)当两物体相撞时,会进入 OnTriggerEnte ...

  3. 1.Spring——七大主要模块

    Spring有七大功能模块,分别是Spring Core,AOP,ORM,DAO,MVC,WEB,Content. 下面分别简单介绍: 1.Spring Core Core模块是Spring的核心类库 ...

  4. 将Excel的数据导入DataGridView中(转)

    https://www.cnblogs.com/lhxhappy/archive/2008/11/26/1341873.html /// <summary> /// 点击按钮导入数据 // ...

  5. [C# | WinCE | Solution] 在 WinCE 上访问 SSL 加密后的 WCF SOAP 服务接口出现“未能与远程服务器建立信任关系”

    Scenario: 服务器的 SOAP 使用了 GeoTrust 签名的 EV 证书,WinCE调用时出现“未能与远程服务器建立信任关系”的错误.原因是该 WinCE 设备信任的证书包括 Global ...

  6. 关于Entity Framework关系配置,提示列名XXXX_Id无效的问题

    问题描述 : 数据库中有两张表,如下: Member(会员)表有外键RoleId,对应的是Role(角色)表的主键Id,业务逻辑是Member表的RoleId必须与Role表的Id对应(但在设计数据表 ...

  7. 使用xtrabackup不停服务做从库

    一.安装xtrabackup 1.YUM安装,下载percona源: yum install http://www.percona.com/downloads/percona-release/redh ...

  8. Win7系统安装 Photoshop CC 中文完全破解版

    Win7系统安装 Photoshop CC 中文完全破解版 谨以此教程献给某位做UI设计的朋友^_^! 01. 确定自己的系统是32位的还是64位的,本人电脑是64位 02. 将 Photoshop_ ...

  9. php算法基础----时间复杂度和空间复杂度

    算法复杂度分为时间复杂度和空间复杂度. 其作用: 时间复杂度是指执行算法所需要的计算工作量: 而空间复杂度是指执行这个算法所需要的内存空间. (算法的复杂性体现在运行该算法时的计算机所需资源的多少上, ...

  10. Hadoop HBase概念学习系列之HBase里的宽表设计概念(表设计)(二十七)

    在下面这篇博文里,我给各位博客们,分享了创建HBase表,但这远不止打好基础. HBase编程 API入门系列之create(管理端而言)(8) 在关系型数据库里,表的高表和宽表是不存在的.在如HBa ...