Nginx 配置TCP代理轉(zhuǎn)發(fā)的實現(xiàn)
使用Nginx 新版的 stream方式,實現(xiàn)TCP/UDP代理轉(zhuǎn)發(fā)。
在Nginx安裝了 stream 模塊后。
修改nginx.conf
在主配置文件下 nginx.conf 增加 stream配置。
如 nginx home 為 /opt/software/nginx
cd /opt/software/nginx/conf vim nginx.conf
增加如下配置
stream {
log_format proxy '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
access_log /opt/software/nginx/logs/tcp-access.log proxy ;
open_log_file_cache off;
# 統(tǒng)一放置,方便管理
include tcpConf/*.conf;
}
增加stream配置
創(chuàng)建tcp配置文件夾,方便以后統(tǒng)一管理配置文件:
cd /opt/software/nginx/conf mkdir tcpConf
創(chuàng)建配置文件
cd tcpConf vim tcp9004.conf
編輯如下:
upstream tcp9004 {
server 118.178.188.188:8992;
}
server {
listen 9004;
proxy_connect_timeout 8s;
proxy_timeout 24h;
proxy_pass tcp9004;
}
測試重啟Nginx
測試Nginx
cd /opt/software/nginx ./sbin/nginx -t
沒有問題之后
重啟Nginx
./sbin/nginx -s reload
端口監(jiān)聽
使用tcpdump監(jiān)聽tcp數(shù)據(jù)
安裝tcpdump
yum install -y tcpdump
查看網(wǎng)卡
ifconfig
監(jiān)聽對應(yīng)網(wǎng)卡上的端口
tcpdump -n -v -i eth1 port 8992
到此這篇關(guān)于Nginx 配置TCP代理轉(zhuǎn)發(fā)的實現(xiàn)的文章就介紹到這了,更多相關(guān)Nginx TCP代理轉(zhuǎn)發(fā)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
keepalived+nginx實現(xiàn)雙服務(wù)器主備方案
本文主要介紹了使用keepalived和nginx實現(xiàn)雙服務(wù)器主備方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12

