nginx實(shí)現(xiàn)TCP反向代理的示例代碼
當(dāng)前實(shí)驗(yàn)環(huán)境:
nginx已安裝版本1.11.13
需要?jiǎng)討B(tài)擴(kuò)展安裝模塊nginx_tcp_proxy_module,實(shí)現(xiàn)tcp反向代理
實(shí)驗(yàn)步驟:
1、nginx當(dāng)前版本1.11.13(nginx已安裝)
# /alidata/nginx/sbin/nginx -v nginx version: nginx/1.13.7
2、查看之前的安裝模塊
# /alidata/nginx/sbin/nginx -V nginx version: nginx/1.13.7 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/alidata/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/src/pcre-8.12/ --add-module=/soft/soft/ngx_http_substitutions_filter_module/ --add-module=/soft/soft/ngx_http_google_filter_module
3、進(jìn)入nginx源碼安裝包
# cd /usr/src/nginx-1.13.7/ # ls auto CHANGES.ru configure html Makefile objs README CHANGES conf contrib LICENSE man pcre-8.12.tar.gz src
4、動(dòng)態(tài)增加tcp反向代理模塊–with-stream=dynamic (這個(gè)模塊和第三方模塊nginx_tcp_proxy_module-master是一樣的)
# ./configure --prefix=/alidata/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/src/pcre-8.12/ --add-module=/soft/soft/ngx_http_substitutions_filter_module/ --add-module=/soft/soft/ngx_http_google_filter_module --with-stream=dynamic # make #這里只做編譯,千萬(wàn)不要make install
5、查看當(dāng)前目錄的 objs/ 目錄下會(huì)生成一個(gè).so文件[ngx_stream_module.so]
# ls objs/ addon nginx ngx_auto_headers.h ngx_stream_module_modules.c src autoconf.err nginx.8 ngx_modules.c ngx_stream_module_modules.o Makefile ngx_auto_config.h ngx_modules.o ngx_stream_module.so
6、在nginx的安裝目錄下創(chuàng)建modules目錄,并將這個(gè).so文件移動(dòng)到 modules目錄下
# cd /alidata/nginx/ # mkdir modules #存在就不用創(chuàng)建了 # chown nginx.nginx modules # cp /usr/src/nginx-1.13.7/objs/ngx_stream_module.so /alidata/nginx/modules/
7、將模塊加載到nginx主配置文件中,并添加tcp的配置
# vi /alidata/nginx/conf/nginx.conf
load_module modules/ngx_stream_module.so; #加載模塊
events {
......
}
#-------------------- HTTP -------------------------------
http {
......
}
#tcp和http是同等級(jí)的,這里只做tcp反向代理配置
#-------------------- TCP/UDP -------------------------------
#include /alidata/nginx/tcp.d/*.conf; #也可以將tcp的配置指向單獨(dú)的配置文件
stream { #stream是固定寫法,代表這是tcp協(xié)議,如果是通過(guò)第三方模塊【nginx_tcp_proxy_module】安裝的這里就換成tcp
upstream proxy_swoole {
server 172.16.100.17:8090;
}
server {
listen 10001; #訪問(wèn)http://ip:10001 跳轉(zhuǎn)到172.16.100.17:8089
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass proxy_swoole;
}
}
說(shuō)明一下 tcp 和 stream 的區(qū)別:
tcp { #tcp表示通過(guò)第三方模塊傳統(tǒng)安裝nginx_tcp_proxy_module
upstream cluster {
server localhost:2000;
server localhost:3000;
}
server{
listen 8080;
proxy_pass cluster;
}
}
--------------------------------------------------------------------------
stream { #stream表示通過(guò)第三方模塊動(dòng)態(tài)安裝 --with-stream=dynamic
upstream cluster {
server localhost:2000;
server localhost:3000;
}
server{
listen 8080;
proxy_pass cluster;
}
}
8、重新加載nginx服務(wù)
# /alidata/nginx/sbin/nginx -s reload # netstat -npult|grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 34716/nginx tcp 0 0 0.0.0.0:10001 0.0.0.0:* LISTEN 34716/nginx tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 34716/nginx
9、訪問(wèn)http://172.16.12.9:10001,能訪問(wèn)到說(shuō)明跳轉(zhuǎn)成功
到此這篇關(guān)于nginx實(shí)現(xiàn)TCP反向代理的示例代碼的文章就介紹到這了,更多相關(guān)nginx TCP反向代理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx rewrite和proxy_pass的區(qū)別及說(shuō)明
這篇文章主要介紹了Nginx rewrite和proxy_pass的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Nginx搶購(gòu)限流配置實(shí)現(xiàn)解析
這篇文章主要介紹了Nginx搶購(gòu)限流配置實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08
Nginx?403?forbidden錯(cuò)誤的原因以及解決方法
yum安裝nginx,安裝一切正常,但是訪問(wèn)時(shí)報(bào)403 forbidden,下面這篇文章主要給大家介紹了關(guān)于Nginx?403?forbidden錯(cuò)誤的原因以及解決方法,需要的朋友可以參考下2022-08-08
nginx?ingress代理websocket流量的配置方法
ingress?nginx默認(rèn)支持websocket協(xié)議,使用長(zhǎng)連接協(xié)議時(shí)需要注意連接超時(shí)的設(shè)置,文中有提到讀取和發(fā)送超時(shí)的注解參數(shù),通過(guò)本文閱讀可以快速掌握,對(duì)nginx?ingress代理websocket相關(guān)知識(shí)感興趣的朋友一起看看吧2022-03-03
使Nginx服務(wù)器支持中文URL的相關(guān)配置詳解
這篇文章主要介紹了使Nginx服務(wù)器支持中文URL的相關(guān)配置方法,搜索引擎方面Google目前對(duì)中文URL的支持度也很好,需要的朋友可以參考下2016-01-01
結(jié)合 Nginx 將 DoNetCore 部署到 阿里云的安裝配置方法
這篇文章主要介紹了結(jié)合 Nginx 將 DoNetCore 部署到 阿里云的方法 ,需要的朋友可以參考下2018-10-10

