Nginx to build a TCP proxy server _nginx

Source: Internet
Author: User

Nginx is not only an HTTP proxy server, it can also be easily built into a TCP proxy server.

First, let's look at the new development version of the build method

1. Installation

> wget http://nginx.org/download/nginx-1.9.0.tar.gz
> Tar zxvf nginx-1.9.0.tar.gz

Version Requirements 1.9.0+

2, configuration

Worker_processes Auto;
Error_log/var/log/nginx/error.log info;
Stream {
  Upstream backend {
    hash $remote _addr consistent;
    Server backend1.example.com:12345 weight=5;
    Server 127.0.0.1:12345      max_fails=3 fail_timeout=30s;
    Server unix:/tmp/backend3;
  }
  server {
    listen 12345;
    Proxy_connect_timeout 1s;
    Proxy_timeout 3s;
    Proxy_pass backend;
  }
  server {
    Listen [:: 1]:12345;
    Proxy_pass unix:/tmp/stream.socket
  }
}

3. Supplementing
Now the Nginx 1.9 is a development version, the current stable version does not have the stream function, but in the next stable release, this feature will be integrated in. Therefore, the recommended use of HTTP proxy students can consider switching to TCP proxy, if only to do a simple agent, and performance will be more excellent.

Second, the old version of the build method

Nginx TCP proxy functionality is provided by the Nginx_tcp_proxy_module module, while monitoring backend host status. The modules include the following modules: Ngx_tcp_module, Ngx_tcp_core_module, Ngx_tcp_upstream_module, Ngx_tcp_proxy_module, Ngx_tcp_upstream_ip_ Hash_module.
1. Installation

# 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
Install

2. Configure

HTTP {
  listen;
  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
  }
}

One problem with this is that the TCP connection will fall off the line. The reason is that when the service side closes the connection, the client cannot immediately detect that the connection has been turned off, and waits until nginx the server-side link is closed when the check rule is executed, and Nginx closes the connection to the client.

3. Maintain Connection Configuration

HTTP {
  listen;
  location/status {
    check_status
  }
}
TCP {
 timeout 1d;
  Proxy_read_timeout 10d;
  Proxy_send_timeout 10d;
  Proxy_connect_timeout;
  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 module Instruction specific see: http://yaoweibin.github.io/nginx_tcp_proxy_module/README.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.