先下载nginx_tcp_proxy_module模块。
- wget https://github.com/yaoweibin/nginx_tcp_proxy_module/archive/master.zip
-
先看看自己原来nginx安装有哪些模块(编译参数):
[root@111]# /tengine/sbin/nginx -V
Tengine version: Tengine/2.1.1 (nginx/1.6.2)
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
TLS SNI support enabledconfigure arguments: –prefix=/tengine –user=www –group=www –with-pcre=../pcre –with-http_ssl_module –with-http_stub_status_module –with-openssl=../openssl –with-zlib=../zlib-1.2.8 –with-http_upstream_check_module –with-http_concat_module –with-syslog –with-backtrace_module –with-http_realip_module –add-module=../ngx_cache_purge –add-module=../ngx_http_accounting_module
- 复制一下,进入到nginx的源码目录
- patch -p1 < /path/to/nginx_tcp_proxy_module/tcp.patch
- ./configure –prefix=/usr/local/nginx –user=www –group=www –with-pcre=../pcre –with-http_ssl_module –with-http_stub_status_module –with-openssl=../openssl –with-zlib=../zlib-1.2.8 –with-http_upstream_check_module –with-http_concat_module –with-syslog –with-backtrace_module –with-http_realip_module –add-module=../ngx_cache_purge –add-module=../ngx_http_accounting_module –add-module=/path/to/nginx_tcp_proxy_module
- make
- cp objs/nginx /usr/local/nginx/sbin/nginx
- 假定你有两个服务需要通过Nginx的Tcp(8888)端口进行负载均衡,参考如下配置:
Shell代码
- tcp {
- upstream cluster {
- # simple round-robin
- server 192.168.0.1:80;
- server 192.168.0.2:80;
- check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
- #check interval=3000 rise=2 fall=5 timeout=1000 type=ssl_hello;
- #check interval=3000 rise=2 fall=5 timeout=1000 type=http;
- #check_http_send “GET / HTTP/1.0\r\n\r\n”;
- #check_http_expect_alive http_2xx http_3xx;
- }
- server {
- listen 8888;
- proxy_pass cluster;
- }
- }
/usr/local/nginx/sbin/nginx -s reload
/tengine/conf/tcp_proxy/socket.xxx.com
nginx.conf增加一条include tcp_proxy/*;
- cat socket.xxx.com
- tcp {
- upstream socket_xxx_com {
- server 1.1.1.1:8888;
- check interval=3000 rise=2 fall=5 timeout=1000; #check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
- }
- server {
- listen 8888;
- proxy_pass socket_xxx_com;
- }
- }