Nginx 配置TCP代理

Nginx 1.9 版本以后增加了stream模块,可以对tcp,udp请求进行代理和负载均衡了,今天来体验一下
首先编译安装过程configure的时候增加选项 --with-stream --with-stream_ssl_module 是支持tcp代理
安装完成后修改 nginx.conf 在文件最后添加 include tcp/*.conf ,注意不是在http范畴内,然后在conf目录下创建tcp目录


简单测试

都在一个机器上, nginx监听 10000 端口,然后开两个窗口,用nc监听 10001 、10002端口。
nginx配置

              / nc -l 10001
telnet--> nginx(10000)  ---->
              \ nc -l 10002
配置

stream {undefined
  upstream backend {undefined
    server 127.0.0.1:10001;
    server 127.0.0.1:10002;
  }
  server {undefined
    listen 10000;
#    listen 10000 udp; 如需代理udp端口则在端口后面加udp即可
    proxy_timeout 20s;
    proxy_pass backend;
  }
}

telnet 127.0.0.1 10000
第一次会代理到 10001 端口,第二次会到 10002 端口,代理正常。


下面是一个ssl 然后ip hash方式的负载均衡(tcp也支持几种负载均衡方式 round-robin, least_conn least_time, hash等)

stream {undefined
  upstream backend {undefined
    hash $remote_addr;
    server 192.168.1.10:20001;
    server 192.168.1.10:20002;
  }
  server {undefined
    listen 20000 ssl;
    ssl_certificate /data/keys/CAcert.pem;
    ssl_certificate_key /data/keys/privkey.pem;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 2h;
    proxy_timeout 20s;
    proxy_pass backend;
  }
}

===================

Nginx 在1.9.0版本发布以前如果要想做到基于TCP的代理及负载均衡需要通过打名为 nginx_tcp_proxy_module 的第三方patch来实现,该模块的代码托管在github上网址:https://github.com/yaoweibin/nginx_tcp_proxy_module/。

Nginx 从1.9.0开始发布ngx_stream_core_module模块,该模块支持tcp代理及负载均衡。

今天我们就要来简单测试一下 Nginx 的 ngx_stream_core_module 模块。

安装Nginx并启用模块

ngx_stream_core_module这个模块并不会默认启用,需要在编译时通过指定--with-stream参数来激活这个模块。

  • 编译安装

 
  1. $ yum -y install proc* openssl* pcre*

  2. $ wget http://nginx.org/download/nginx-1.9.4.tar.gz

  3. $ tar zxvf nginx-1.9.4.tar.gz

  4. $ cd nginx-1.9.4

  5. $ ./configure  --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'

  6. $ make

  7. $ make install

  • 配置stream模块

实例一:测试MYSQL负载均衡

stream模块必需在nginx.conf中配置

 
  1. $ mv nginx.conf{,.bak}

  2. $ vim  /etc/nginx/nginx.conf

  3. worker_processes auto;

  4. events {

  5.    worker_connections  1024;

  6. }

  7. error_log /var/log/nginx_error.log info;

  8. stream {

  9.    upstream mysqld {

  10.        hash $remote_addr consistent;

  11.        server 192.168.1.42:3306 weight=5 max_fails=1 fail_timeout=10s;

  12.        server 192.168.1.43:3306 weight=5 max_fails=1 fail_timeout=10s;

  13.    }

  14.    server {

  15.        listen 3306;

  16.        proxy_connect_timeout 1s;

  17.        proxy_timeout 3s;

  18.        proxy_pass mysqld;

  19.    }

  20. }

实例二:实现SSH转发

 
  1. upstream ssh {

  2.        hash $remote_addr consistent;

  3.        server 192.168.1.42:22 weight=5;

  4.   }

  5. server {

  6.    listen 2222;

  7.    proxy_pass ssh;    

  8.   }

实例三:官方一个较完整的配置示例

stream模块的配置里还支持类似server unix:/tmp/backend3.sock;这样的sock数据交换接口,也可以直接proxy_pass unix:/tmp/stream.socket;

 
  1. worker_processes auto;

  2. error_log /var/log/nginx/error.log info;

  3. events {

  4.    worker_connections  1024;

  5. }

  6. stream {

  7.    upstream backend {

  8.        hash $remote_addr consistent;

  9.        server backend1.example.com:12345 weight=5;

  10.        server 127.0.0.1:12345            max_fails=3 fail_timeout=30s;

  11.        server unix:/tmp/backend3;

  12.    }

  13.    server {

  14.        listen 12345;

  15.        proxy_connect_timeout 1s;

  16.        proxy_timeout 3s;

  17.        proxy_pass backend;

  18.    }

  19.    server {

  20.        listen [::1]:12345;

  21.        proxy_pass unix:/tmp/stream.socket;

  22.    }

  23. }

ngx_stream_core_module也同样的支持tcp长连接保持。keepidle是保持时间,keepintvl是间隔时间 ,keepcnt是发送的个数。

so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值