基于nginx和tengine的tcp反向代理,负载均衡 安装和配置

时间:2022-02-08 10:22:23

先下载nginx_tcp_proxy_module模块。

    1. wget https://github.com/yaoweibin/nginx_tcp_proxy_module/archive/master.zip
    2. 先看看自己原来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 enabled

      configure 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

    3. 复制一下,进入到nginx的源码目录
    4. patch -p1 < /path/to/nginx_tcp_proxy_module/tcp.patch
    5. ./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
    6. make
    7. cp  objs/nginx /usr/local/nginx/sbin/nginx
    8. 假定你有两个服务需要通过Nginx的Tcp(8888)端口进行负载均衡,参考如下配置:
      Shell代码  基于nginx和tengine的tcp反向代理,负载均衡 安装和配置
      1. tcp {
      2. upstream cluster {
      3. # simple round-robin
      4. server 192.168.0.1:80;
      5. server 192.168.0.2:80;
      6. check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
      7. #check interval=3000 rise=2 fall=5 timeout=1000 type=ssl_hello;
      8. #check interval=3000 rise=2 fall=5 timeout=1000 type=http;
      9. #check_http_send “GET / HTTP/1.0\r\n\r\n”;
      10. #check_http_expect_alive http_2xx http_3xx;
      11. }
      12. server {
      13. listen 8888;
      14. proxy_pass cluster;
      15. }
      16. }

      /usr/local/nginx/sbin/nginx -s reload

    9. /tengine/conf/tcp_proxy/socket.xxx.com

    10. nginx.conf增加一条include tcp_proxy/*;

    11. cat socket.xxx.com
    12. tcp {
    13. upstream socket_xxx_com {
    14. server 1.1.1.1:8888;
    15. check interval=3000 rise=2 fall=5 timeout=1000; #check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
    16. }
    17. server {
    18. listen 8888;
    19. proxy_pass socket_xxx_com;
    20. }
    21. }