需求產生背景
最近同事在測試和生產環境中分別部署了一套應用,由于應用只能集成ldap,而我們公司使用的是ad,于是我搭建了一個openldap服務,賬號先通過lsc從ad同步到openldap,然后使用saslauthd傳遞身份驗證到ad。在測試環境中我們的應用能夠連接ldap登錄,但是在生產環境中卻無法訪問到openldap服務器,我不想重復的在生產環境重新安裝維護一套openldap服務,這個過程比較繁瑣,還需通過定時任務每天讓ad的賬號與openldap同步,于是我想這可以通過端口轉發實現,節點之間的拓撲大概像下面這樣。
為什么要使用nginx實現
在百度上搜索端口轉發,實現的方式有很多種,多數都是通過iptables實現,但是我分別在ubuntu和centos服務器上測試過都沒生效,無奈選擇nginx來實現
使用nginx實現端口轉發
如果我們的操作系統比較新,可以直接直接通過軟件源安裝nginx,只要nginx版本大于1.9默認是支持tcp代理的。
檢查nginx是否支持tcp代理
1
|
nginx -v |
當我們在輸出的配置參數中包含
--with-stream
說明nginx是支tcp代理的
安裝依賴
rhel/centos/fedora
1
|
yum install -y pcre* openssl* |
debian/ubuntu
1
|
apt-get install zlib1g-dev libpcre++-dev openssl |
下載依賴
1
2
|
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz tar -zxvf ngx_cache_purge-2.3.tar.gz -c /usr/ local /src |
下載并安裝
下載源碼包
1
|
wget http://nginx.org/download/nginx-1.9.9.tar.gz |
解壓
1
|
tar -zxf nginx-1.9.9.tar.gz |
編譯安裝
1
2
3
4
5
|
cd nginx-1.9.9 ./configure --prefix=/usr/local/nginx \ --add-module=/usr/local/src/ngx_cache_purge-2.3 \ --with-http_stub_status_module --with-stream make && make install; |
修改配置文件
/usr/local/nginx/conf/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
events { ... } stream { upstream ldap { hash $remote_addr consistent; server 192.168.1.8:389; } server { listen 1389; proxy_connect_timeout 5s; proxy_timeout 5s; proxy_pass ldap; } } http { ... } |
這個示例我們將本地的1389端口轉發到192.168.1.8的389端口上
啟動并檢查服務是否正常
啟動nginx服務
1
|
/usr/ local /nginx/sbin/nginx |
檢查nginx進程
1
|
netstat -anput | grep nginx |
到此這篇關于使用nginx實現端口轉發tcp代理的實現示例的文章就介紹到這了,更多相關nginx端口轉發tcp代理內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/jack170601/article/details/122114545