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. php操作文件类的函数

    <?php /** // 一行一行读取一个文件 (文件内容很大的时候,适用.file_get_contents此场景就不太好) $re = fopen("index.php" ...

  2. iOS9中http不能使用的解决

    用xcode7写程序的时候发现webview不能显示http的链接网页,发现原来是由于ios9的一个新特性,iOS9引入了新特性App Transport Security (ATS),新特性要求Ap ...

  3. bitmap过大无法显示图片的问题 - z

    public Bitmap ratio(Bitmap image, float pixelW, float pixelH) {Bitmap bitmap = null;try {ByteArrayOu ...

  4. Android提权原理

    Android的内核就是Linux,所以Android获取root其实和Linux获取root权限是一回事儿. 你想在Linux下获取root权限的时候就是执行sudo或者su,接下来系统会提示你输入 ...

  5. 【调试】Core Dump是什么?Linux下如何正确永久开启?

    [调试]Core Dump是什么?Linux下如何正确永久开启?

  6. ZAB协议和Paxos算法

    前言在上一篇文章Paxos算法浅析中主要介绍了Paxos一致性算法应用的场景,以及对协议本身的介绍:Google Chubby是一个分布式锁服务,其底层一致性实现就是以Paxos算法为基础的:但这篇文 ...

  7. Microsoft Dynamics CRM 增删改子表汇总子表的某个字段到主表的某个字段(通用插件)

    背景 经常有某个汇总子表的数量到主表的总数量,或者汇总子表的总价到主表的总价这种需求. 传统的做法: 1.就是为每个子表实体单独写成一个插件,但是这样不好复用. 2.主表的汇总字段是汇总货币类型,但是 ...

  8. Kaggle: Google Analytics Customer Revenue Prediction EDA

    前言 内容提要 本文为Kaggle竞赛 Google Analytics Customer Revenue Prediction 的探索性分析 题目要求根据历史顾客访问GStore的数据,预测其中部分 ...

  9. 记录:tf.saved_model 模块的简单使用(TensorFlow 模型存储与恢复)

    虽然说 TensorFlow 2.0 即将问世,但是有一些模块的内容却是不大变化的.其中就有 tf.saved_model 模块,主要用于模型的存储和恢复.为了防止学习记录文件丢失或者蠢笨的脑子直接遗 ...

  10. 关于Java开发一职的经验

    本人为大四软件工程学生,由于准备不充分也没有前人指点,去年10月份才赶上秋招节奏,然后签下了一家比较起来还行的公司.所以不太期望大家有求职意愿但苦于不知作何准备,所以特列以下知识点检索供大家查阅.如果 ...