Nginx TCP協議


https://blog.51cto.com/moerjinrong/2287680

 --很好

Nginx 配置TCP/UDP端口轉發

nginx 的功能非常強大,其中作為代理服務器是非常常用的功能,但是之前的nginx代理只能做七層代理,也就說是基於應用層面的代理,TCP層面的代理一般會配合haproxy 來使用。但是自從nginx 1.9 以后通過stream模塊實現了tcp 代理功能,無需其他軟件配合即可實現四層代理和七層代理,即:訪問該服務器的指定端口,nginx就可以充當端口轉發的作用將流量導向另一個服務器,同時獲取目標服務器的返回數據並返回給請求者。nginx的TCP代理功能跟nginx的反向代理不同的是:請求該端口的所有流量都會轉發到目標服務器,而在反向代理中可以細化哪些請求分發給哪些服務器;另一個不同的是,nginx做TCP代理並不僅僅局限於WEB的URL請求,還可以轉發如memcached、MySQL等點到點的請求

環境

ip 主機名 端口 說明
192.168.1.101 node1 3389 nginx服務器
192.168.1.102 node2 ~ 客戶端
8.8.8.8 ~ 389 目標服務器

1.安裝nginx服務

1.1 安裝nginx

默認安裝stream模塊,我寫文檔時nginx版本為1.14.0
參考:RHEL/CentOS 安裝 nginx

1.2 對於已經安裝nginx的,檢查是否編譯時帶with-stream參數

[root@node1 ~]# nginx -V |grep with-stream
  • 1.
 
 

有with-stream參數,可以代理tcp協議

2 配置nginx的tcp代理

請注意,stream塊和http塊是兩個不同的模塊,stream不屬於http模塊,即不能放到/etc/nginx/conf.d/,stream是通過tcp層轉發,而不是http轉發。

如配置在http內,啟動nginx會報如下錯誤:

nginx: [emerg] "server" directive is not allowed here
  • 1.
 
 

2.1 修改主配置文件,添加stream目錄

[root@node1 ~]# cd /etc/nginx/ [root@node1 ~]# cp -a nginx.conf{,_$(date +%F)} [root@node1 ~]# vim nginx.conf # 最后追加如下內容 # tcp/ip proxy include /etc/nginx/tcp.d/*.conf;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
 
 

2.2 添加tcp轉發配置

[root@node1 ~]# mkdir tcp.d [root@node1 ~]# cd tcp.d
  • 1.
  • 2.
 
 

在新建的 tcp.d 目錄下創建 conf 文件新建一個 tcp 配置,例如我轉發到IP為8.8.8.8的389端口

[root@node1 ~]# vim openldap.conf stream{ upstream tcpssh{ hash $remote_addr consistent; server 8.8.8.8:389 max_fails=3 fail_timeout=10s; } server{ listen 3389; proxy_connect_timeout 20s; proxy_timeout 5m; proxy_pass tcpssh; } }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
 
 

說明:

  • "upstream tcpssh":轉發的目的地址和端口等設置;其中tcpssh為自定義;
  • "server":提供轉發的服務,即訪問localhost:3389,會跳轉至代理"tcpssh"指定的轉發地址.。

這里就是配置的監聽本地3389端口,會將流量相應轉發到8.8.8.8服務器的389端口上。

測試配置文件是否正確

[root@node1 ~]# nginx -t -c /etc/nginx/nginx.conf [root@node1 ~]# nginx -t -c /etc/nginx/tcp.d/openldap.conf
  • 1.
  • 2.
 
 

2.3 啟動nginx服務

啟動nginx服務

[root@node1 ~]# systemctl start nginx.service
  • 1.
 
 

查看是否啟動

[root@node1 ~]# systemctl status nginx.service ● nginx.service - nginx - high performance web server Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled) Active: active (running) since Sat 2018-09-29 11:34:01 CST; 5h 37min ago Docs: http://nginx.org/en/docs/ Main PID: 26114 (nginx) CGroup: /system.slice/nginx.service ├─26114 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf └─26115 nginx: worker process Sep 29 11:34:01 node1 systemd[1]: Starting nginx - high performance web server... Sep 29 11:34:01 node1 systemd[1]: Started nginx - high performance web server. [root@node1 ~]# 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
 
 

3 客戶端配置

3.1 測試連接目標端口:

[root@node2 ~]# telnet 192.168.1.101 3389 Trying 192.168.1.101... Connected to 192.168.1.101. Escape character is '^]'.
  • 1.
  • 2.
  • 3.
  • 4.
 
 

出現"Connected to 192.168.1.101",說明連接成功
測試完成,"Ctrl+C"結束

3.2 相關業務軟件配置

把要連接8.8.8.8:389的配置改為nginx服務器ip(192.168.1.101),及代理端口3389。

如果業務沒有出現問題的話,則說明已經配置完成了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM