nginx搭建tcpProxy 伺服器_nginx

來源:互聯網
上載者:User

nginx不僅可以是httpProxy 伺服器,也可以輕鬆搭建成tcpProxy 伺服器。

首先我們看下最新開發版的搭建方法

1. 安裝

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

版本要求 1.9.0+

2、配置

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. 補充
現在nginx 1.9是開發版,目前穩定版沒有stream的功能,但在下個的穩定版發布時,這功能就會整合進來。因此推薦以後用http proxy的同學可以考慮換成tcp proxy,如果只是做簡單的代理而已,而且效能上會更優異。

二、老版本的搭建方法

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會關閉與用戶端的串連。

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.