Nginx TCP代理


nginx 在1.9.0 版本發布以前如果要想做到基於TCP的代理及負載均衡需要通過打名為nginx_tcp_proxy_module的第三方patch來實現,該模塊的代碼托管在github上 網址:https://github.com/yaoweibin/nginx_tcp_proxy_module/,而我們今年要來簡單測試一下nginx 的ngx_stream_core_module,該模塊有nginx 官方發布並支持tcp代理及負載均衡。

 

ngx_stream_core_module 這個模塊在1.90版本后將被啟用。但是並不會默認安裝,需要在編譯時通過指定 --with-stream 參數來激活這個模塊。

其他改進包括:

  • Change: 刪除過時的 aio 和 rtsig 事件處理方法
  • Feature: 可在 upstream 塊中使用 "zone" 指令
  • Feature: 流模塊,支持 TCP 代理和負載均衡
  • Feature: ngx_http_memcached_module 支持字節范圍
  • Feature: Windows 版本支持使用共享內存,帶隨機化地址空間布局.
  • Feature: "error_log" 指令可在 mail 和 server 級別
  • Bugfix: the "proxy_protocol" parameter of the "listen" directive did not work if not specified in the first "listen" directive for a listen socket.

簡單配置步驟如下(測試MYSQL負載):

wget http://nginx.org/download/nginx-1.9.3.tar.gz
tar zxvf nginx-1.9.3.tar.gz
cd nginx-1.9.3yum -y install proc*
yum -y install openssl*
yum -y install pcre*

./configure --prefix=/usr/local/nginx-1.9.3_tcp \
--with-http_ssl_module --with-http_spdy_module \
--with-http_stub_status_module --with-pcre \
--with-stream

cd /usr/local/nginx-1.9.3_tcp/conf/
mv nginx.conf{,.bak}
vim nginx.conf


worker_processes auto;
events {
    worker_connections  1024;
}
error_log /var/log/nginx_error.log info;
stream {
    upstream mysqld {
        hash $remote_addr consistent;
        server 192.168.1.42:3306 weight=5 max_fails=1 fail_timeout=10s;
        server 192.168.1.43:3306 weight=5 max_fails=1 fail_timeout=10s;
    }
 
    server {
        listen 3306;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass mysqld;
    }
 
}

另外還可以實現一個ssh 轉發代理

upstream ssh {
        hash $remote_addr consistent;
        server 192.168.1.42:22 weight=5;
   } 
 
server {
    listen 2222;
    proxy_pass ssh;    
   }  

 

參考:

http://zhangge.net/5037.html

http://nginx.org/en/docs/stream/ngx_stream_core_module.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM