您的位置:首页 > 理论基础 > 计算机网络

Nginx基于TCP的负载均衡的配置例子

2017-06-01 18:13 423 查看
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负载写一起达到多重目的。

Linux下Nginx+Tomcat负载均衡和动静分离配置要点  http://www.linuxidc.com/Linux/2016-01/127255.htm

Docker+Nginx+Tomcat7配置简单的负载均衡  http://www.linuxidc.com/Linux/2015-12/125907.htm

Nginx负载均衡(主备)+Keepalived  http://www.linuxidc.com/Linux/2015-12/126865.htm

使用Nginx作为负载均衡器 http://www.linuxidc.com/Linux/2015-12/125789.htm

CentOS环境下Nginx实现3台虚拟机负载均衡 http://www.linuxidc.com/Linux/2015-12/125875.htm

Nginx反向代理负载均衡群集实战  http://www.linuxidc.com/Linux/2015-08/122111.htm

Nginx 的详细介绍请点这里
Nginx 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-02/128062.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: