Nginx 在1.9.0版本發佈之前若是要想作到基於TCP的代理及負載均衡須要經過打名爲 nginx_tcp_proxy_module
的第三方patch來實現,該模塊的代碼託管在github上網址:https://github.com/yaoweibin/nginx_tcp_proxy_module/。html
Nginx 從1.9.0開始發佈ngx_stream_core_module
模塊,該模塊支持tcp代理及負載均衡。mysql
今天咱們就要來簡單測試一下 Nginx 的 ngx_stream_core_module
模塊。
安裝Nginx並啓用模塊nginx
ngx_stream_core_module
這個模塊並不會默認啓用,須要在編譯時經過指定--with-stream
參數來激活這個模塊。git
編譯安裝github
$ yum -y install proc* openssl* pcre* $ wget http://nginx.org/download/nginx-1.9.4.tar.gz $ tar zxvf nginx-1.9.4.tar.gz $ cd nginx-1.9.4 $ ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' $ make $ make install
配置stream模塊sql
實例一:測試MYSQL負載均衡vim
stream模塊必需在nginx.conf中配置負載均衡
$ mv nginx.conf{,.bak} $ vim /etc/nginx/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轉發dom
upstream ssh { hash $remote_addr consistent; server 192.168.1.42:22 weight=5; } server { listen 2222; proxy_pass ssh; }
實例三:官方一個較完整的配置示例ssh
stream模塊的配置裏還支持相似server unix:/tmp/backend3.sock;
這樣的sock數據交換接口,也能夠直接proxy_pass unix:/tmp/stream.socket;
。
worker_processes auto; error_log /var/log/nginx/error.log info; events { worker_connections 1024; } 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; } }
ngx_stream_core_module也一樣的支持tcp長鏈接保持。keepidle是保持時間,keepintvl是間隔時間 ,keepcnt是發送的個數。
so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]
參考文檔
http://www.google.com http://zhangge.net/5037.html http://nginx.org/en/docs/stream/ngx_stream_core_module.html