反代理netty时需要用到ngx_stream_core_module这个模块,nginx1.9以上自带,需要重新编译加上
例子:
注意:以下代码放http节点外
负载均衡版
- stream{
- upstream socket_proxy {
- hash $remote_addr consistent;
- server 127.0.0.1:6688 weight=5 max_fails=3 fail_timeout=30s;
-
- server 127.0.0.1:7777 weight=5 max_fails=3 fail_timeout=30s; #多机情况下就是负载均衡
- }
-
- server {
-
- listen 8899;
- proxy_connect_timeout 5s;
- proxy_timeout 5s;
- proxy_pass socket_proxy ;
-
- }
- }
复制代码
普通版:
- stream{
- upstream socket_proxy {
- # hash $remote_addr consistent;
- server 127.0.0.1:6688;
-
-
- }
-
- server {
-
- listen 8899;
- proxy_connect_timeout 20s;
- proxy_timeout 20s;
- proxy_pass socket_proxy ;
-
- }
- }
复制代码
说明:客户端访问8899端口时,转发给6688端口,测试工具SocketTool4 |