nginx1.9.0 for tcp来了
Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:新浪、网易、腾讯等。
nginx-1.9.0 mainline version has been released, with the stream module for generic TCP proxying and load balancing. nginx-1.9.0
Changes with nginx 1.9.0 28 Apr 2015
*) Change: obsolete aio and rtsig event methods have been removed.
*) Feature: the "zone" directive inside the "upstream" block.
*) Feature: the stream module.
*) Feature: byte ranges support in the ngx_http_memcached_module.
Thanks to Martin Mlynář.
*) Feature: shared memory can now be used on Windows versions with
address space layout randomization.
Thanks to Sergey Brester.
*) Feature: the "error_log" directive can now be used on mail and server
levels in mail proxy.
*) Bugfix: the "proxy_protocol" parameter of the "listen" directive did
not work if not specified in the first "listen" directive for a
listen socket.
测试实验:
客户端发送2次hello srv给nginx,nginx轮询分发给server1和server2,再把不同的应答返回给客户端。
client1——\(\)62345\($----server1(12345)\)\(\)\(\)\(|---nginx----| client2------\)\(\)\(\)$$—-server2(12346)
1)nginx 部署轮询方式连接两个tcp服务后端。
#nginx.conf
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 127.0.0.1:12345;
server 127.0.0.1:12346;
#server unix:/tmp/backend3;
}
server {
listen 62345;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass backend;
}
}
2)服务端:启动两个server
server.pl 127.0.0.1 12345
server.pl 127.0.0.1 12346
#!/usr/bin/perl -w
use IO::Socket;
use IO::Select;
main:
{
return -1 if(@ARGV < 2);
my $host = $ARGV[0];
my $port = $ARGV[1];
my $socket = IO::Socket::INET->new(
LocalAddr => "$host",
LocalPort => "$port",
Type => SOCK_STREAM,
Proto => "tcp",
Listen => 200, #定义listen的最大数
Blocking => 0, #非阻塞
) or die "Can not create socket connect.$@";
my $sel = IO::Select->new($socket);
while (my @ready = $sel->can_read) {
foreach my $fh (@ready) {
if ($fh == $socket) {
my $new = $socket->accept();
$sel->add($new);
}else {
$fh->recv($buffer,1024,0); #接收客户端消息
print "$buffer \n";
$fh->send("hello from ".$host.":".$port."\n",0); #发送服务端消息
$fh->autoflush(1);
$sel->remove($fh);
$fh->close();
}
}
}
$socket->close() or warn "Close Socket failed.$@";
}
3)客户端:启动两次client
client.pl 127.0.0.1 62345
#!/usr/bin/perl -w
use strict;
use IO::Socket;
main:
{
return -1 if(@ARGV < 2);
my $host = $ARGV[0];
my $port = $ARGV[1];
my $sock = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port, Proto => 'tcp');
$sock or die "no socket :$!";
my $msg;
$sock->send("hello srv");
$sock->recv($msg, 1024);
print $msg . "\n";
close $sock;
}
测试结果:
启动两次client,分别收到server1,server2的应答:
hello from 127.0.0.1:12345
hello from 127.0.0.1:12346
正常测试到nginx该版本TCP proxying功能。