1、操作背景

操作系统版本:CentOS Linux release 7.4. (Core)
nginx版本:1.13.4 nginx从1.9.0版本开始,新增了ngx_stream_core_module模块,使nginx支持四层负载均衡。默认编译的时候该模块并未编译进去,需要编译的时候添加--with-stream,使其支持stream代理。

2、nginx编译添加stream模块

2.1、查看原nginx编译参数

[root@test-server sbin]# nginx -V
nginx version: nginx/1.13.
built by gcc 4.8. (Red Hat 4.8.-) (GCC)
built with OpenSSL 1.0.2k-fips Jan
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-pcre=/usr/local/src/pcre-8.41

2.2、添加stream模块进行重新编译

此处nginx源码目录为:/usr/local/src/nginx-1.13.4,即为编译命令执行目录。

编译命令如下:
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-pcre=/usr/local/src/pcre-8.41 --with-stream

2.3、进行make操作

此处nginx源码目录为:/usr/local/src/nginx-1.13.,即为编译命令执行目录。
make 此处一定不能使用make install命令,执行该命令会将原有nginx目录进行覆盖。

3、关停nginx同时复制新的nginx启动文件

关闭nginx服务
systemctl stop nginx 备份原有nginx二进制文件。
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-no-strem 复制新编译好的nginx二进制文件。从此处nginx源码目录为:/usr/local/nginx-1.13.4。即为编译命令执行目录。
cp ./objs/nginx /usr/local/nginx/sbin/nginx

4、启动测试 

启动nginx。
systemctl start nginx 查看nginx模块信息。
nginx -V
nginx version: nginx/1.13.
built by gcc 4.8. (Red Hat 4.8.-) (GCC)
built with OpenSSL 1.0.2k-fips Jan
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/usr/local/nginx/tmp/client/ --http-proxy-temp-path=/usr/local/nginx/tmp/proxy/ --http-fastcgi-temp-path=/usr/local/nginx/tmp/fcgi/ --with-poll_module --with-file-aio --with-http_realip_module --with-http_addition_module --with-http_addition_module --with-http_random_index_module --with-http_stub_status_module --http-uwsgi-temp-path=/usr/local/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/nginx/scgi_temp --with-pcre=/usr/local/src/pcre-8.41 --with-stream 可以看到stream模块已经编译到nginx内了。

5、nginx stream模块配置简析

stream段的配置要与http段在同级目录。此处引用的为官方nginx说明配置。
stream {
upstream backend {
hash $remote_addr consistent; server backend1.example.com: weight=;
server 127.0.0.1: max_fails= fail_timeout=30s;
server unix:/tmp/backend3;
} upstream dns {
server 192.168.0.1:;
server dns.example.com:;
} server {
listen ;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass backend;
} server {
listen 127.0.0.1: udp reuseport;
proxy_timeout 20s;
proxy_pass dns;
} server {
listen [::]:;
proxy_pass unix:/tmp/stream.socket;
}
}

举一个栗子,利用stream模块代理 zk服务的2181端口。

stream {
upstream zk_server {
server 172.16.3.8:2181 weight=5;
}
server {
listen 2181 tcp;
proxy_responses 1;
proxy_timeout 20s;
proxy_pass zk_server;
}
}

6、编译nignx systemd服务启动文件

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target [Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true [Install]

编译nginx平滑添加stream模块的更多相关文章

  1. 线上nginx 平滑添加新模块;如(--with-http_realip_module)

    nginx 添加模块1.查看当前nginx信息(配置文件路径,启动用户...) ps aux | grep nginx 2.查看当前nginx已启用的模块(记录模块信息,安装路径)./nginx -V ...

  2. nginx 动态添加ssl模块

    一.查看nginx模块 /usr/local/nginx/sbin/nginx -V 二.安装openssl包 yum -y install pcre  pcre-devel zlib  zlib-d ...

  3. nginx 重装添加http_ssl_module模块

    起因: 如果出现“[emerg] 10464#0: unknown directive "ssl" in /usr/local/nginx-0.6.32/conf/nginx.co ...

  4. 玩玩 Nginx 2-----给Nginx添加第三方模块(动态更新upstream)

          接上一篇,我们在初始化安装的时候添加了nginx_lua模块,然后了解到nginx不可以动态加载模块,所以当你安装第三方模块的时候需要覆盖nginx文件.接下来一起看看如何安装nginx第 ...

  5. Nginx基于TCP/UDP端口的四层负载均衡(stream模块)配置梳理

    通过我们会用Nginx的upstream做基于http/https端口的7层负载均衡,由于Nginx老版本不支持tcp协议,所以基于tcp/udp端口的四层负载均衡一般用LVS或Haproxy来做.至 ...

  6. yum安装的Nginx添加第三方模块支持tcp

    需求:生产有个接口是通过socket通信.nginx1.9开始支持tcp层的转发,通过stream实现的,而socket也是基于tcp通信. 实现方法:Centos7.2下yum直接安装的nginx, ...

  7. nginx 番外----添加第三方模块

    #第三方模块需要先进行下载,然后再编译时指定文件目录 1.查看当前编译模块 root@nginx sbin]# ./nginx -V #查看当前添加模块 nginx version: nginx/ b ...

  8. yum安装下的nginx,如何添加模块,和添加第三方模块

    需求:生产有个接口是通过socket通信.nginx1.9开始支持tcp层的转发,通过stream实现的,而socket也是基于tcp通信. 实现方法:Centos7.2下yum直接安装的nginx, ...

  9. Deepin15.11源码安装Nginx17.5包括stream模块和njs模块

    一:先到官网下载nginx-1.17.5.tar.gz包并且解压到当前目录,解压后目录为:nginx-1.17.5: 二:下载njs源码(它没有像stream模块一样附带在了nginx源码里),因此首 ...

随机推荐

  1. 树莓派学习笔记(4):利用yeelink实现在线硬件状态监控

    转载请注明:@小五义http://www.cnblogs.com/xiaowuyi 一.实验目的 本文实验目的是定时获取树莓派CPU的温度.占用率及内存占用率,并其结果上传到yeelink网站,实现在 ...

  2. php操作url 函数等

    pathinfo() - Returns information about a file path parse_str() - Parses the string into variables pa ...

  3. URL Encode

    用VC实现post数据常常会遇到URL编码问题 在此封装一个工具类进行UTF8编码的转换 源码来自php source code 只是简单的封装给C++调用   //URL 编解码类 //来自PHP源 ...

  4. Spring Data JPA、MyBatis还有Hibernate有什么区别

    原文:https://www.imooc.com/article/19754?block_id=tuijian_wz Spring Data JPA.MyBatis还有Hibernate有什么区别 2 ...

  5. ORM框架-SQLAchemy使用

    一.ORM简介 orm英文全称object relational mapping,就是对象映射关系程序,简单来说我们类似python这种面向对象的程序来说一切皆对象,但是我们使用的数据库却都是关系型的 ...

  6. python基础2之字符串、列表、字典、集合

    内容概要: 一.python2 or 3 二.字符串拼接 三.字符串 四.列表.元祖 五.字典 六.集合 七.练习 一.python2 or python3 目前大多使用python2.7,随着时间的 ...

  7. C语言如何向系统接要存

    C语言如何向系统接要存,就有这么三种方式: 1.向栈要. 2.向堆要. 3.向数据段要. 这一下就扯出了三种内存空间,内存空间的本质是一样的,一个地址对应一个方框,方框里可以放数据.但是为了更好的去 ...

  8. flask-login 整合 pyjwt + json 简易flask框架

    现在很多框架都实现前后端分离,主要为了适应以下几个目的: 1,前后端的分离,可以使前端开发和后端开发更加分工明确,而不是后端还需要在视图模板中加入很多{% XXXX %}标签 2,是为了适应跨域调用或 ...

  9. Jmeter+ant+Jenkins构建接口自动化测试

    1.已写好jmeter脚本 2.安装ant并将ant-jmeter-1.1.1.jar文件放入ant/lib目录,用于调用jmeter 3.修改jmeter的jmeter.properties文件(将 ...

  10. 浅谈SVG(可缩放的矢量图形)

    前一段项目中用到了svg图片就和其他的元素一样 直接引用就可以展示在页面上,因为项目紧张没有仔细的研究,最近在扩展自己的基础知识,偶然看到了这个东西,于是总结了一些博客园中关于这个svg的基础知识,只 ...