nginx如何支持tcp轉發

nginx支持tcp轉發,在1.9.0版本及之後版本中提供,涉及核心模塊:ngx_stream_core_modul。nginx若要使用該功能,需要在nginx編譯時,帶上--with-stream配置參數以啓用他。

nginx官網stream模塊鏈接:

nginx所有模塊官方文檔鏈接: ngx_docs
ngx_stream核心模塊官方文檔鏈接: ngx_stream_core_module

 

 

nginx http模塊 conf 結構:

http {
    upstream {
    }
    server {
        location {
        }
    }
}

nginx tcp模塊 conf 結構,注意沒有location

stream {
    upstream {
    }
    server {
    }
}

測試案例(linux環境):

1、nginx編譯增加--with-stream配置參數,參考:https://blog.csdn.net/jwentao01/article/details/95341994

2、創建nginx/conf/module目錄,並在該目錄下創建nginx_tcp.conf,內容如下:

stream {
    upstream tcp-test {
        # 存在另一個測試環境172.16.2.211,監聽2379
        # telnet 172.16.2.211 2379 可通
        server 172.16.2.211:2379;
    }

    server {
        # tcp 監聽端口配置爲7999
        listen       7999;
        proxy_pass   tcp-test;
    }
}

3、nginx.conf中配置include nginx_tcp.conf:

...

include module/nginx_tcp.conf;

http {

...

4、啓動nginx

5、執行 telnet 127.0.0.1 7999 可通,nginx轉發tcp到了172.16.2.211:2379,驗證成功,執行結果:

[root@host-*-*-*-* /]# telnet 127.0.0.1 7999
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
^]
telnet> Connection closed.

 

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