nginx tcp代理

默北 Nginx1467,81111字数 1463阅读4分52秒阅读模式

nginx tcp代理功能由nginx_tcp_proxy_module模块提供,同时监测后端主机状态。该模块包括的模块有: ngx_tcp_module, ngx_tcp_core_module, ngx_tcp_upstream_module, ngx_tcp_proxy_module, ngx_tcp_upstream_ip_hash_module。

1. 安装

# wget http://nginx.org/download/nginx-1.4.4.tar.gz
# tar zxvf nginx-1.4.4.tar.gz
# cd nginx-1.4.4
# ./configure --add-module=/path/to/nginx_tcp_proxy_module
# make
# make install

2. 配置

http {
    listen 80;
    location /status {
        check_status;
    }
}
tcp {
    upstream cluster_www_ttlsa_com {
        # simple round-robin
        server 127.0.0.1:1234;
        check interval=3000 rise=2 fall=5 timeout=1000;
        #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_www_ttlsa_com;
    }
}

这会出现一个问题,就是tcp连接会掉线。原因在于当服务端关闭连接的时候,客户端不可能立刻发觉连接已经被关闭,需要等到当Nginx在执行check规则时认为服务端链接关闭,此时nginx会关闭与客户端的连接。文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-tcp-proxy/

3. 保持连接配置

http {
    listen 80;
    location /status {
        check_status;
    }
}
tcp {
	timeout 1d;
    proxy_read_timeout 10d;
    proxy_send_timeout 10d;
    proxy_connect_timeout 30;
    upstream cluster_www_ttlsa_com {
        # simple round-robin
        server 127.0.0.1:1234;
        check interval=3000 rise=2 fall=5 timeout=1000;
        #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_www_ttlsa_com;
		so_keepalive on;
        tcp_nodelay on;
    }
}

nginx_tcp_proxy_module模块指令具体参见: http://yaoweibin.github.io/nginx_tcp_proxy_module/README.html文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-tcp-proxy/ 文章源自运维生存时间-https://www.ttlsa.com/nginx/nginx-tcp-proxy/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 12/01/2014 16:00:40
  • 转载请务必保留本文链接:https://www.ttlsa.com/nginx/nginx-tcp-proxy/
  • nginx
  • nginx_tcp_proxy_module
  • tcp代理
评论  14  访客  11
    • 匿名
      匿名 9

      按文章设置好后,用telnet连nginx代理端口失败,日志记录错误为upstream servers are busy or encounter error! while connecting to upstream. client: xxxxxx, server xxxxx

        • 匿名
          匿名 9

          @ 匿名 我也遇到了,重启之后正常了。

        • […] 16.?CDN调度器HAProxy、Nginx、Varnish 17.?lnmp架构下php安全配置分享 18.?nginx tcp代理 19.?nginx正向代理 20.?搭建nginx反向代理用做内网域名转发 […]

          • 运维生存时间网友
            运维生存时间网友 9

            patch -p1 < /path/to/nginx_tcp_proxy_module/tcp.patch
            这个还没加

            • 匿名
              匿名 9

              patch -p1 < /path/to/nginx_tcp_proxy_module/tcp.patch
              这个还没加

              • xxx
                xxx 9

                你这个 nginx_tcp_proxy_module 这个module 哪儿来的?貌似要下载一个 yaoweibin 的module才可以吧

                  • 管理员
                    管理员 5

                    @ xxx nginx1.9已经支持tcp,不需要再使用这个模块

                  • ggggggggg
                    ggggggggg 9

                    hggg

                    • Aceslup
                      Aceslup 9

                      proxy_pass cluster_www_ttlsa_com,这个真的不需要http:// 跟着吗?

                        • Aceslup
                          Aceslup 9

                          @ Aceslup 这个只是提供检查功能的么?

                          • 河西
                            河西 9

                            @ Aceslup 只是一个名称而已

                        评论已关闭!