nginx支持tcp转发,在1.9.0版本及之后版本中提供,涉及核心模块:ngx_stream_core_modul。nginx若要使用该功能,需要在nginx编译时,带上--with-stream配置参数以启用他。
nginx官网stream模块链接:
nginx所有模块官方文档链接: | ngx_docs |
ngx_stream核心模块官方文档链接: | ngx_stream_core_module |
nginx http模块 conf 结构:
http {
upstream {
}
server {
location {
}
}
}
nginx tcp模块 conf 结构,注意没有location:
stream {
upstream {
}
server {
}
}
测试案例(linux环境):
1、nginx编译增加--with-stream配置参数,参考:https://blog.csdn.net/jwentao01/article/details/95341994
2、创建nginx/conf/module目录,并在该目录下创建nginx_tcp.conf,内容如下:
stream {
upstream tcp-test {
# 存在另一个测试环境172.16.2.211,监听2379
# telnet 172.16.2.211 2379 可通
server 172.16.2.211:2379;
}
server {
# tcp 监听端口配置为7999
listen 7999;
proxy_pass tcp-test;
}
}
3、nginx.conf中配置include nginx_tcp.conf:
...
include module/nginx_tcp.conf;
http {
...
4、启动nginx
5、执行 telnet 127.0.0.1 7999 可通,nginx转发tcp到了172.16.2.211:2379,验证成功,执行结果:
[root@host-*-*-*-* /]# telnet 127.0.0.1 7999
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
^]
telnet> Connection closed.