nginx的平滑升级以及4层代理

nginx平滑升级

随着网站并发访问量越来越高,nginx web服务器也越来越流行,nginx版本换代越来越频繁,新版本的nginx更新了许多新功能,例如stream四层代理功能。伴随着nginx的广泛应用,版本升级必然是越来越快的,线上业务不能停,此时nginx的升级就是运维的重要工作了,下面就带大家一起来理解下nginx平滑升级。

1、nginx平滑升级原理

多进程模式下的请求分配方式:
Nginx默认工作在多进程模式下,即主进程(master process)启动后完成配置加载和端口绑定等动作,fork出指定数量的工作进程(worker process),这些子进程会持有监听端口的文件描述符(fd),并通过在该描述符上添加监听事件来接受连接(accept)。
信号的接收和处理
Nginx主进程在启动完成后会进入等待状态,负责响应各类系统消息,例如SIGCHLD、SIGHUP、SIGUSR2等。
Nginx信号简介

  • 主进程支持的信号
    TERM, INT: 立刻退出
    QUIT: 等待工作进程结束后再退出
    KILL: 强制终止进程
    HUP: 重新加载配置文件,使用新的配置启动工作进程,并逐步关闭旧进程。
    USR1: 重新打开日志文件
    USR2: 启动新的主进程,实现热升级
    WINCH: 逐步关闭工作进程
  • 工作进程支持的信号
    TERM, INT: 立刻退出
    QUIT: 等待请求处理结束后再退出
    USR1: 重新打开日志文件
2、nginx平滑升级实验(版本1.15.9升到1.16.0)

(1)旧版本信息查看

[root@localhost ~] nginx -V			#查看当前nginx的配置功能
nginx version: nginx/1.15.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module

(2)新版本的安装
注意./configure完后不能make install 否则会将原来的nginx文件全部覆盖

[root@localhost ~] tar xf nginx-1.16.0.tar.gz -C /usr/src/
[root@localhost ~] cd /usr/src/nginx-1.16.0/
[root@localhost nginx-1.16.0] ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-pcre && make

(3)备份二进制文件,用新版本的替换

[root@localhost nginx-1.16.0] cd /usr/local/nginx/sbin
[root@localhost sbin] mv nginx nginx.old
[root@localhost sbin] cp /usr/src/nginx-1.16.0/objs/nginx ./

(4)确保我们的配置文件没有报错

[root@localhost sbin] nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

如果你的ngins服务允许重启的话,实际上到这咱们的这个nginx就已经升级级完毕了,但我们要做的是平滑升级所以不能重启,。
(5)发送USR2信号
向主进程(master)发送USR2信号,nginx会启动一个新版本的master进程和对应工作进程,同旧版本一起来处理请求。

[root@localhost ~] ps aux | grep nginx
root      10351  0.0  0.1  45864  1128 ?        Ss   16:45   0:00 nginx: master process nginx
nginx     10352  0.0  0.2  48404  2228 ?        S    16:45   0:00 nginx: worker process
root      13249  0.0  0.0 112724   988 pts/1    R+   17:12   0:00 grep --color=auto nginx
[root@localhost ~] kill -USR2 10351
[root@localhost ~] ps aux | grep nginx			#如果没有出现新的进程那么升级失败,只能去重启
root      10351  0.0  0.1  45864  1128 ?        Ss   16:45   0:00 nginx: master process nginx
nginx     10352  0.0  0.2  48404  2228 ?        S    16:45   0:00 nginx: worker process
root      11497  0.0  0.2  45864  1131 ?        Ss   16:47   0:00 nginx: master process nginx
nginx     11498  0.0  0.3  48404  3292 ?        S    16:47   0:00 nginx: worker process
root      13249  0.0  0.0 112724   988 pts/1    R+   17:12   0:00 grep --color=auto nginx

(6)发送WHICH信号
像旧的nginx主进程(master)发送which信号,它会逐步关闭自己的工作进程(主进程不会退出),这是所有的请求都会交给新版本的nginx来去处理。

[root@localhost ~] kill -WHICH 10351
[root@localhost ~] ps aux | grep nginx
root      10351  0.0  0.1  45864  1128 ?        Ss   16:45   0:00 nginx: master process nginx
root      11497  0.0  0.2  45864  1131 ?        Ss   16:47   0:00 nginx: master process nginx
nginx     11498  0.0  0.3  48404  3292 ?        S    16:47   0:00 nginx: worker process
root      13249  0.0  0.0 112724   988 pts/1    R+   17:12   0:00 grep --color=auto nginx

注意
如果此时咱们想要去回滚继续使用旧的版本的话,可向旧的nginx主进程发送HUP信号,它会重新启动工作进程,仍使用旧版本的配置文件。然后可以将新版nginx进程杀死,使用QUIT,TERM或KILL即可。

[root@localhost ~] kill -HUP 10351

(7)发送QUIT信号
升级你完毕,咱们可向旧的nginx主进程(master)发送QUIT信号,是旧的主进程退出。

[root@localhost ~] kill -QUIT 10351
root      11497  0.0  0.2  45864  1131 ?        Ss   16:47   0:00 nginx: master process nginx
nginx     11498  0.0  0.3  48404  3292 ?        S    16:47   0:00 nginx: worker process
root      13249  0.0  0.0 112724   988 pts/1    R+   17:12   0:00 grep --color=auto nginx

(8)验证nginx版本号

[root@localhost ~] nginx -v
nginx version: nginx/1.16.0

nginx的4层代理

1、七层代理回顾

我们先来回顾一下nginx七层的反向代理,再去介绍4层代理。

linux 平滑升级nginx nginx平滑升级原理_平滑升级


我们通过简单地步骤进行回顾nginx反向代理的过程 --> 1.5上安装lamp --> 1.3安装nginx --> 配置个PHP的网页 --> 分别用浏览器访问验证nginx、php、httpd --> 修改nginx配置文件(如下代码) --> 再去验证反向代理。

location ~* \.php${
	proxy_pass http://192.168.1.5;
}
2、nginx四层代理

nginx 从1.9.0版本开始支持四层代理,但做四层代理时 编译需要添加 –with-stream模块
实例:公司原有的测试数据库在主机192.168.1.3上边,现在数据库转移到了192.168.1.4上,为了不让各个地方都需要更改地址,现在需要一个四层代理工具,将原来请求到192.168.10.5的3306端口转发到192.168.10.4的3306端口。这就用到了nginx的四层代理。
安装的时候加上–with-stream这个模块

[root@localhost nginx-1.16.0] ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-pcre --with-stream && make && make install
[root@localhost nginx-1.16.0] vim /usr/local/nginx/conf/nginx.conf
#注意stream和http是同一个级别的所以要对其写在一起
stream {
	server {
	    listen 2222;
	    proxy_pass 192.168.1.4:22;
	}
}
http {

开另外一个shell

ssh 192.168.1.3:2222

linux 平滑升级nginx nginx平滑升级原理_平滑升级_02


实际上我们连接的是1.4这台主机。

linux 平滑升级nginx nginx平滑升级原理_nginx四层代理_03