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

相关推荐
菜只因C10 分钟前
深入剖析嵌入式系统:从基础到实践的全面指南
大数据·网络·人工智能
courniche36 分钟前
CSMA/CA与CSMA/CD的区别
网络·网络协议·信息与通信·信号处理
niuTaylor1 小时前
嵌入式工程师多线程编程(二)生产者-消费者模式
网络·多线程
yangshuo12813 小时前
实战代码:esp32-cam按钮控制手机拍照V1.0
网络·智能手机
christine-rr4 小时前
密码学基础——分组密码的运行模式
网络·密码学·密码
张一不吃豆芽4 小时前
TCPIP详解 卷1协议 三 链路层
网络·网络协议·tcp/ip
珹洺4 小时前
Linux红帽:RHCSA认证知识讲解(十 二)调试 SELinux,如何管理 SELinux 的运行模式、安全策略、端口和上下文策略
linux·运维·服务器·网络·安全
千羽星弦4 小时前
Kubernetes 集群搭建(一):k8s 从环境准备到 Calico 网络插件部署(1.16版本)
网络·容器·kubernetes
陈大大陈5 小时前
自制简易 Shell:像搭建积木小屋一样打造命令交互小天地
linux·运维·服务器·c++·tcp/ip·microsoft·交互
微风轻吟挽歌5 小时前
HTTP 308 错误永久重定向 (Permanent Redirect)
网络·网络协议·http