昨天在公司微信群,CTO 分享了这个消息,对运维来说以后基于 TCP 协议的后端业务的高可用又多了一个新的选择,实在是棒极了!
一直以来,Nginx 并不支持 tcp 协议,所以后台的一些基于 TCP 的业务就只能通过其他高可用负载软件来完成了,比如 Haproxy。
这算是一个 nginx 比较明显的缺憾。不过,在 1.90 发布后这个认知将得到改写:
2015-04-28 | nginx-1.9.0 mainline version has been released, with the stream module for generic TCP proxying and load balancing.
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.
所以,我们如果需要用到这个功能,就需要加上 --with-stream 参数重新编译 nginx。对于已在线上运行的 nginx,你可能要用到平滑升级来避免线上的服务被中断,可以参考张戈以前分享的教程:
最后贴一下官方分享的 stream 模块的简单配置 demo:
worker_processes auto; error_log /var/log/nginx/error.log info; 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; } }
和 http 模块类似,简单明了。相信熟悉 nginx 的朋友很容易的就能完成一个 nginx 下的 TCP 负载均衡集群配置。
由于工作繁忙,实在是心有余而力不足。还好最近公司给我招了个小鲜肉来做运维助理,等空下来了,我再去测一测这个 Nginx 的 TCP 代理和负载均衡功能。到时候再来博客分享一二,敬请期待!
等到稳定性好了再考虑升级 :grin:
继续再等等
已经编译安装了ngx_stream_core_module,就等你的教程啦,顺便也编译了stream_ssl_module
我用的稳定版1.10.2版本的nginx tcp为什么连接不能保持?
proxy_timeout 1800s; #这是半小时超时,自己看着设置。
多谢博主回复,按照你的配置连接可以保持了,怎么让它连接一直保持呢?还有它的so_keepalived中的超时时间有什么用?
有这个参数?没用过。
超时你设置为0看看,是不是永久保持了。
stream和http是并行的是嘛,为啥stream里面配置log_format一直提示不让配置呢
stream {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
upstream back {
server 11.21.47.54:9000;
}
upstream backed {
server 11.21.47.82:9100;
}
server {
listen 9000;
proxy_connect_timeout 60s;
proxy_timeout 60s;
proxy_pass ora;
access_log /usr/local/nginx/logs/access.log main;
}
server {
listen 9100;
proxy_connect_timeout 60s;
proxy_timeout 60s;
proxy_pass eam;
access_log /usr/local/nginx/logs/access.log main;
}
}
log_format 是放在http部分吧,http{
log_format ....
}
stream 没研究过日志参数
博主,用这个模块转发,源那边有办法获取到用户IP嘛。