nginx通過四層代理實現(xiàn)端口轉(zhuǎn)發(fā)的示例代碼
公司原有的測試數(shù)據(jù)庫在主機192.168.10.5
上邊,現(xiàn)在數(shù)據(jù)庫轉(zhuǎn)移到了192.168.10.4
上,為了不讓各個地方都需要更改地址,現(xiàn)在需要一個四層代理工具,將原來請求到192.168.10.5
的3306
端口轉(zhuǎn)發(fā)到192.168.10.4
的3306
端口。
這個工具,用到了 nginx 的四層代理。
官方文檔:http://nginx.org/en/docs/stream/ngx_stream_core_module.html
四層代理依賴模塊ngx_stream_core_module
,該模塊自 1.9.0 版開始可用。默認情況下,此模塊不構(gòu)建,應(yīng)使用配置參數(shù)啟用 --with-stream
。
安裝過程簡示:
[root@linux-node1 src]# tar xf nginx-1.10.3.tar.gz [root@linux-node1 src]# cd nginx-1.10.3 [root@linux-node1 nginx-1.10.3]# useradd -s /sbin/nologin -M www [root@linux-node1 nginx-1.10.3]# yum install gcc gcc-c++ zlib-devel pcre-devel openssl openssl-devel -y [root@linux-node1 nginx-1.10.3]# ./configure --prefix=/usr/local/nginx-1.10.3 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-stream [root@linux-node1 nginx-1.10.3]# make && make install
可以通過nginx -V
查看一下是否將上述模塊編譯進來,如果沒有,可以重新編譯一下。
來到主配置:
worker_processes 1; events { worker_connections 1024; } stream { upstream tcp_proxy { hash $remote_addr consistent; #遠程地址做個hash server 192.168.10.4:22; } server { listen 2222; proxy_connect_timeout 1s; proxy_timeout 10s; #后端連接超時時間 proxy_pass tcp_proxy; } }
此配置是將本機的 2222 端口轉(zhuǎn)發(fā)到 192.168.10.4 的 22 端口,配置之后,試驗一下:
[root@7-3 nginx]$ssh -p 2222 root@192.168.10.5 The authenticity of host '[192.168.10.5]:2222 ([192.168.10.5]:2222)' can't be established. ECDSA key fingerprint is 05:2f:63:e9:87:be:b4:44:d3:d7:77:a0:52:e0:4f:2f. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '[192.168.10.5]:2222' (ECDSA) to the list of known hosts. root@192.168.10.5's password: Last login: Wed Nov 7 15:24:33 2018 from 192.168.10.1 [root@7-2 ~]$hostname -I 192.168.10.4
剛剛設(shè)置了 10 的超時,如果需要的話,可以將之注釋掉。
同理,配置數(shù)據(jù)庫端口的轉(zhuǎn)發(fā)也就非常簡單了:
worker_processes 1; events { worker_connections 1024; } stream { upstream tcp_proxy { hash $remote_addr consistent; #遠程地址做個hash server 192.168.10.4:3306; } server { listen 3306; proxy_connect_timeout 1s; # proxy_timeout 10s; #后端連接超時時間 proxy_pass tcp_proxy; } }
這樣一來,用戶連接192.168.10.5:3306
的時候,就會被轉(zhuǎn)發(fā)到192.168.10.4:3306
了。
到此這篇關(guān)于nginx通過四層代理實現(xiàn)端口轉(zhuǎn)發(fā)的文章就介紹到這了,更多相關(guān)nginx 端口轉(zhuǎn)發(fā)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解nginx實現(xiàn)https網(wǎng)站設(shè)置
這篇文章主要介紹了詳解nginx實現(xiàn)https網(wǎng)站設(shè)置,詳細的介紹了HTTPS簡介和證書生成等,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06Nginx如何配置Http、Https、WS、WSS的方法步驟
這篇文章主要介紹了Nginx如何配置Http、Https、WS、WSS的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Nginx記錄分析響應(yīng)慢的請求及替換網(wǎng)站響應(yīng)內(nèi)容的配置
這篇文章主要介紹了Nginx記錄分析響應(yīng)慢的請求及替換網(wǎng)站響應(yīng)內(nèi)容的配置,分別用到了ngx_http_log_request_speed模塊和ngx_http_sub_module模塊,需要的朋友可以參考下2016-01-01使用Nginx+Tomcat實現(xiàn)負載均衡的全過程
很多用到nginx的地方都是作為靜態(tài)伺服器,這樣可以方便緩存那些靜態(tài)文件,比如CSS,JS,html,htm等文件,下面這篇文章主要給大家介紹了關(guān)于使用Nginx+Tomcat實現(xiàn)負載均衡的相關(guān)資料,需要的朋友可以參考下2022-05-05