nginx實現(xiàn)TCP反向代理的示例代碼
當(dāng)前實驗環(huán)境:
nginx已安裝版本1.11.13
需要動態(tài)擴(kuò)展安裝模塊nginx_tcp_proxy_module,實現(xiàn)tcp反向代理
實驗步驟:
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、動態(tài)增加tcp反向代理模塊–with-stream=dynamic (這個模塊和第三方模塊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 #這里只做編譯,千萬不要make install
5、查看當(dāng)前目錄的 objs/ 目錄下會生成一個.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目錄,并將這個.so文件移動到 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是同等級的,這里只做tcp反向代理配置 #-------------------- TCP/UDP ------------------------------- #include /alidata/nginx/tcp.d/*.conf; #也可以將tcp的配置指向單獨的配置文件 stream { #stream是固定寫法,代表這是tcp協(xié)議,如果是通過第三方模塊【nginx_tcp_proxy_module】安裝的這里就換成tcp upstream proxy_swoole { server 172.16.100.17:8090; } server { listen 10001; #訪問http://ip:10001 跳轉(zhuǎn)到172.16.100.17:8089 proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass proxy_swoole; } }
說明一下 tcp 和 stream 的區(qū)別:
tcp { #tcp表示通過第三方模塊傳統(tǒng)安裝nginx_tcp_proxy_module upstream cluster { server localhost:2000; server localhost:3000; } server{ listen 8080; proxy_pass cluster; } } -------------------------------------------------------------------------- stream { #stream表示通過第三方模塊動態(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、訪問http://172.16.12.9:10001,能訪問到說明跳轉(zhuǎn)成功
到此這篇關(guān)于nginx實現(xiàn)TCP反向代理的示例代碼的文章就介紹到這了,更多相關(guān)nginx TCP反向代理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx rewrite和proxy_pass的區(qū)別及說明
這篇文章主要介紹了Nginx rewrite和proxy_pass的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06Nginx?403?forbidden錯誤的原因以及解決方法
yum安裝nginx,安裝一切正常,但是訪問時報403 forbidden,下面這篇文章主要給大家介紹了關(guān)于Nginx?403?forbidden錯誤的原因以及解決方法,需要的朋友可以參考下2022-08-08nginx?ingress代理websocket流量的配置方法
ingress?nginx默認(rèn)支持websocket協(xié)議,使用長連接協(xié)議時需要注意連接超時的設(shè)置,文中有提到讀取和發(fā)送超時的注解參數(shù),通過本文閱讀可以快速掌握,對nginx?ingress代理websocket相關(guān)知識感興趣的朋友一起看看吧2022-03-03使Nginx服務(wù)器支持中文URL的相關(guān)配置詳解
這篇文章主要介紹了使Nginx服務(wù)器支持中文URL的相關(guān)配置方法,搜索引擎方面Google目前對中文URL的支持度也很好,需要的朋友可以參考下2016-01-01結(jié)合 Nginx 將 DoNetCore 部署到 阿里云的安裝配置方法
這篇文章主要介紹了結(jié)合 Nginx 將 DoNetCore 部署到 阿里云的方法 ,需要的朋友可以參考下2018-10-10