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

/ nginx / 没有评论 / 4148浏览

编译安装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

1

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 img img 进入nginx-1.12.1,然后编译命令用(./configure)加上之前服务器的参数 img

./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 5 就可以在objs下看到nginx了,执行命令./nginx –V查看编译后的模块 img

--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;
    }
}