编译安装nginx添加stream模块支持tcp(stream)代理

从1.9.0版开始,NGX_流_核心_模块可用。这个模块在默认情况下不是构建的,应该使用--with-stream配置参数来启用它。(1.9.0之前的版本则不支持stream,也就是不支持tcp模块)

1、下载和当前版本一致的nginx包(tar.gz包)

nginx官网地址:https://nginx.org/

2、查看版本和现有的模块

进入到nginx应用目录 我的是:cd /usr/local/nginx-1.12.1/sbin查看查看版本和现有的模块./nginx -V

f3fad32c8f3500548dcd257243a7bc424c0.jpg

nginx version: nginx/1.12.1

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)

built with OpenSSL 1.0.2h  3 May 2016

TLS SNI support enabled

configure arguments:

--prefix=/usr/local/nginx-1.12.1

--conf-path=/usr/local/nginx-1.12.1/conf/nginx.conf --pid-path=/usr/local/nginx-1.12.1/logs/nginx.pid --error-log-path=/usr/local/nginx-1.12.1/logs/error.log --http-log-path=/usr/local/nginx-1.12.1/logs/access.log

--with-http_ssl_module

--with-pcre=/usr/local/src/pcre-8.32

--add-module=/usr/local/src/nginx-sticky

--with-zlib=/usr/local/src/zlib-1.2.8

--with-openssl=/usr/local/src/openssl-1.0.2h

--with-ipv6

版本是1.12.1版本大于1.9.0,支持stream模块,以上这些标红的参数都需要用到。

3、解压安装nginx

把nginx-1.12.2.tar.gz放在服务器任意目录下,然后解压:tar –zxvf nginx-1.12.2.tar.gz

17293ba3df26c082476187a5ef136ea48bc.jpg

7153f79ecb2335ace3ab7e7ee42f14f6976.jpg

进入nginx-1.12.1,然后编译命令用(./configure)加上之前服务器的参数

6eeaa0b68f18219d7a5366b970664951b00.jpg

./configure --prefix=/usr/local/nginx-1.12.1 --conf-path=/usr/local/nginx-1.12.1/conf/nginx.conf --pid-path=/usr/local/nginx-1.12.1/logs/nginx.pid --error-log-path=/usr/local/nginx-1.12.1/logs/error.log --http-log-path=/usr/local/nginx-1.12.1/logs/access.log

--with-http_ssl_module

--with-pcre=/usr/local/src/pcre-8.32

--add-module=/usr/local/src/nginx-sticky

--with-zlib=/usr/local/src/zlib-1.2.8

--with-openssl=/usr/local/src/openssl-1.0.2h

--with-ipv6 --with-stream

把要添加stream模块的服务器的日志路径和模块加在 ./configure 的后面把--with-stream加在最后面就可以实现编译添加stream支持tcp了(带path的都是日志路径,--with和--add的后边带路径的都是模块必须要有的没有的去网上下载)[LCJ1] 

等待./configure执行完成后再执行make,然后再执行make install

然后在nginx-1.12.1文件夹下就可以看到objs文件夹了进入objs

b5afe5d6ed240f3ec067947d69f5071dcf6.jpg

就可以在objs下看到nginx了,执行命令 ./nginx –V查看编译后的模块

3a88f807dea0c01c9fca4c22358ffb63118.jpg

--prefix=/usr/local/nginx-1.12.1_tcp --conf-path=/usr/local/nginx-1.12.1_tcp/conf/nginx.conf --pid-path=/usr/local/nginx-1.12.1_tcp/logs/nginx.pid --error-log-path=/usr/local/nginx-1.12.1_tcp/logs/error.log --http-log-path=/usr/local/nginx-1.12.1_tcp/logs/access.log --with-http_ssl_module --with-pcre=/opt/pcre-8.32 --add-module=/opt/nginx-sticky/ --with-zlib=/opt/zlib-1.2.11 --with-openssl=/opt/openssl-1.0.2q --with-ipv6 --with-stream

可以看到已经编译添加stream模块完成

4、备份不带stream模块的nginx

备份之前sbin下的nginx:

cd /usr/local/nginx-1.12.1/sbin(进入sbin目录下)

cp nginx ./nginx.bak(把nginx备份,并命名为nginx.bak)

把带有stream模块的nginx  cp到/usr/local/nginx-1.12.1/sbin目录下

5、重新加载nginx在sbin目录下./nginx –s reload则可以完成平滑添加stream模块

6、stream代理的配置

编译完成后的nginx可以支持tcp的代理了,接下来在/usr/local/nginx-1.12.1/conf下的nginx.conf文件中完成配置。

6.1、tcp负载均衡的配置

支持tcp的stream模块配置和http的配置属于同级

stream {

         upstream backend {

         server 10.20.133.115:9090;

        }

         server {

         listen  test.17ebank.com:9901;

                   allow 121.199.129.16;

                   deny all;

         proxy_pass   backend;

         }

}

6.2、nginx白名单设置

stream {                                              ————支持tcp协议的stream

        upstream backend {

         server 10.20.133.115:9090;

        }

        server {

                 listen  test.17ebank.com:9901;

                 allow 121.199.129.160;      ————只允许这个ip访问

                 deny all;                                           ————拒绝所有ip

                 proxy_pass   backend;

        }

}

转载于:https://my.oschina.net/u/4163854/blog/3074707

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值