nginx实现TCP反向代理的示例代码

 更新时间:2025年01月22日 10:15:38   作者:会飞的爱迪生  
本文主要介绍了nginx实现TCP反向代理的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

当前实验环境:
nginx已安装版本1.11.13
需要动态扩展安装模块nginx_tcp_proxy_module,实现tcp反向代理

实验步骤:

1、nginx当前版本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、进入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、动态增加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、查看当前目录的 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的安装目录下创建modules目录,并将这个.so文件移动到 modules目录下

# cd /alidata/nginx/
# mkdir modules                    #存在就不用创建了
# 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协议,如果是通过第三方模块【nginx_tcp_proxy_module】安装的这里就换成tcp

    upstream proxy_swoole {
        server 172.16.100.17:8090;
    }

    server {
       listen 10001;            #访问http://ip:10001 跳转到172.16.100.17:8089
       proxy_connect_timeout 1s;
       proxy_timeout 3s;
       proxy_pass proxy_swoole;
    }
}

说明一下 tcp 和 stream 的区别:

tcp {                                    #tcp表示通过第三方模块传统安装nginx_tcp_proxy_module

        upstream cluster {
                server localhost:2000;
                server localhost:3000;
        }
        server{
                listen 8080;
                proxy_pass cluster;
        }
}

--------------------------------------------------------------------------
stream {                                  #stream表示通过第三方模块动态安装 --with-stream=dynamic
        upstream cluster {
                server localhost:2000;
                server localhost:3000;
        }
        server{
                listen 8080;
                proxy_pass cluster;
        }
}

8、重新加载nginx服务

# /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,能访问到说明跳转成功

到此这篇关于nginx实现TCP反向代理的示例代码的文章就介绍到这了,更多相关nginx TCP反向代理内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Nginx rewrite和proxy_pass的区别及说明

    Nginx rewrite和proxy_pass的区别及说明

    这篇文章主要介绍了Nginx rewrite和proxy_pass的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-06-06
  • 详解Nginx如何处理WebSocket连接

    详解Nginx如何处理WebSocket连接

    在当今互联网的世界中,实时通信变得越来越重要,WebSocket 作为一种实现实时双向通信的技术,正被广泛应用于各种场景,而 Nginx 作为一款高性能的 Web 服务器和反向代理服务器,在处理 WebSocket 连接方面也有着出色的表现,本文介绍了Nginx如何处理WebSocket连接
    2024-07-07
  • nginx安装第三方模块的方法

    nginx安装第三方模块的方法

    这篇文章主要介绍了nginx安装第三方模块的方法,包含在未安装nginx的情况下安装nginx第三方模块和在已安装nginx情况下安装nginx第三方模块,需要的朋友可以参考下
    2014-06-06
  • Nginx抢购限流配置实现解析

    Nginx抢购限流配置实现解析

    这篇文章主要介绍了Nginx抢购限流配置实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • Nginx 403 forbidden错误的原因以及解决方法

    Nginx 403 forbidden错误的原因以及解决方法

    yum安装nginx,安装一切正常,但是访问时报403 forbidden,下面这篇文章主要给大家介绍了关于Nginx 403 forbidden错误的原因以及解决方法,需要的朋友可以参考下
    2022-08-08
  • nginx ingress代理websocket流量的配置方法

    nginx ingress代理websocket流量的配置方法

    ingress nginx默认支持websocket协议,使用长连接协议时需要注意连接超时的设置,文中有提到读取和发送超时的注解参数,通过本文阅读可以快速掌握,对nginx ingress代理websocket相关知识感兴趣的朋友一起看看吧
    2022-03-03
  • 使Nginx服务器支持中文URL的相关配置详解

    使Nginx服务器支持中文URL的相关配置详解

    这篇文章主要介绍了使Nginx服务器支持中文URL的相关配置方法,搜索引擎方面Google目前对中文URL的支持度也很好,需要的朋友可以参考下
    2016-01-01
  • 结合 Nginx 将 DoNetCore 部署到 阿里云的安装配置方法

    结合 Nginx 将 DoNetCore 部署到 阿里云的安装配置方法

    这篇文章主要介绍了结合 Nginx 将 DoNetCore 部署到 阿里云的方法 ,需要的朋友可以参考下
    2018-10-10
  • Nginx 499错误问题及解决办法

    Nginx 499错误问题及解决办法

    Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器,接下来通过本文给大家介绍Nginx 499错误问题及解决办法,一起看看吧
    2016-06-06
  • nginx目录路径重定向的方法

    nginx目录路径重定向的方法

    这篇文章主要介绍了nginx目录路径重定向的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09

最新评论