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是固定寫(xiě)法,代表這是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實(shí)現(xiàn)http轉(zhuǎn)換為https的項(xiàng)目實(shí)踐
Nginx作為Web服務(wù)器時(shí),可以通過(guò)配置實(shí)現(xiàn)HTTP跳轉(zhuǎn)HTTPS,本文主要介紹了nginx實(shí)現(xiàn)http轉(zhuǎn)換為https的實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03詳解nginx rewrite和根據(jù)url參數(shù)location
本篇文章主要是介紹了nginx rewrite和根據(jù)url參數(shù)location,有興趣的同學(xué)可以了解以下。2016-11-11Nginx域名轉(zhuǎn)發(fā)使用場(chǎng)景代碼實(shí)例
這篇文章主要介紹了Nginx域名轉(zhuǎn)發(fā)使用場(chǎng)景代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Nginx部署Vue項(xiàng)目添加訪問(wèn)后綴方式
文章介紹了如何根據(jù)需要為Vue項(xiàng)目配置不同的訪問(wèn)路徑,并通過(guò)Nginx進(jìn)行相應(yīng)的配置,通過(guò)設(shè)置`vue.config.js`中的`publicPath`和`route`的`base`為`/app`,并將打包后的文件放入指定目錄,然后在Nginx配置中使用`alias`和`try_files`指令來(lái)處理路徑2025-01-01基于Nginx禁止指定IP、國(guó)外IP訪問(wèn)我的網(wǎng)站
這篇文章主要介紹了用Nginx禁止指定IP、國(guó)外IP訪問(wèn)我的網(wǎng)站,想要實(shí)現(xiàn)這個(gè)功能方法有很多種,這里基于 Nginx 的 ngx_http_geoip2 模塊來(lái)禁止國(guó)外 IP 訪問(wèn)網(wǎng)站,需要的朋友可以參考下2022-05-05