nginx1.9實現TCP代理轉發

1.軟件下載(目前最穩定版本):

wget http://nginx.org/download/nginx-1.9.12.tar.gz


2.建立nginx運行用戶和程序目錄:

groupadd www
useradd -s /sbin/nologin -g www www
mkdir -p /usr/local/nginx
mkdir -p /usr/local/pcre
mkdir -p /data/nginxlog/

tar xvf nginx1.9.tar 
cd /workspace/nginx1.9

3.安裝pcre

unzip pcre-8.32.zip
cd pcre-8.32 
./configure --prefix=/usr/local/pcre --enable-utf8 --enable-pcregrep-libbz2 --enable-pcregrep-libz
make && make install 
cd ..
pwd

tar -xvf nginx-1.9.12.tar.gz 
cd nginx-1.9.12
./configure --user=www --group=www \
--prefix=/usr/local/nginx \
--with-pcre \
--with-pcre=/workspace/nginx1.9/pcre-8.32 \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-http_stub_status_module  \
--with-stream \
--with-stream_ssl_module \
--sbin-path=/usr/sbin/nginx \
--error-log-path=/data/nginxlog/error.log \
--http-log-path=/data/nginxlog/access.log

4、nginx做個快捷方式

ln -s /usr/local/nginx/sbin/nginx/ /usr/sbin/

5、啓動nginx

6、實例配置

準備兩個mysql實例,詳細如下:

server 192.168.100.70:3306 ;

server 192.168.100.71:3306 ;

案例如下:

1、nginx 主配置文件nginx.conf添加內容如下:
events {
    worker_connections  1024;
}

stream {
   include /usr/local/nginx/conf/stream_conf/*.conf;
   limit_conn_zone $binary_remote_addr zone=ip_addr:10m; 
   #定義限制IP連接數名稱,與大小 $binary_remote_addr以2進制存放遠程地址
}

2、然後建立相應目錄
mkdir -p /usr/local/nginx/conf/stream_conf/

cd /usr/local/nginx/conf/stream_conf/

3、vim mysql3306.conf配置文件如下:

    upstream db {
        hash $remote_addr consistent;		
        #iphash根據訪問地址分配到固定的後端服務器。
        server 192.168.100.70:3306;
        server 192.168.100.71:3306;
    }
    server {
        listen 3306;
        proxy_pass db;
        proxy_connect_timeout 1s; 	
        #快速故障檢查
        proxy_timeout 3s;			
        #設置超時時間,連接將超時斷開。
        proxy_download_rate 1k;     
        #限制下載速度爲1k
        proxy_upload_rate   10k;    
        #限制上傳速度爲10k
        limit_conn ip_addr 1;       
        ##是限制每個IP只能發起1個連接 (addr 要跟 limit_conn_zone 的變量對應)
        allow 127.0.0.1;			
        #acl,設置允許訪問IP地址;
        deny all;

    }



健康檢查、負載配置參考

https://www.nginx.com/resources/admin-guide/tcp-load-balancing/


資料

nginx 限制

https://www.nginx.com/resources/admin-guide/restricting-access-tcp/

upstream 

https://www.nginx.com/resources/admin-guide/load-balancer

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章