原理
1.nginx的主进程(master process)启动后完成配置加载和端口绑定等动作,fork出指定数量的工作进程(worker process),这些子进程会持有监听端口的文件描述符(fd),并通过在该描述符上添加监听事件来接收连接(accept);
2.nginx主进程在启动完成后会进入等待状态,负责响应各类系统消息,入SIGCHLD、SIGHUP、SIGUSR2等;
3.主进程支持的信号:
TERM,INT:立刻退出;
QUIT:等待工作进程结束后再退出;
KILL:强制终止进程;
HUP:重新加载配置文件,使用新的配置启动工作进程,并逐步支持的信号;
USR1:重新打开日志文件;
USR2:启动新的主进程,实现热升级
WINCH:逐步关闭进程及工作进程支持的信号;
注:USR2信号用于在旧版本进程的基础上,重新生成新版本的进程,但是旧版本进程仅限是nginx命令的绝对路径启动
过程
1.查看旧版nginx的编译参数
2.编译新版本nginx源码包,安装路径需与旧版本一直,注意:不要执行 make install;
3.备份旧版本nginx二进制文件,用新版本的替换;
4.确保配置文件无报错;
5.发送USR2信号:向主进程(master)发送USR2信号,nginx会启动一个新版本的master进程和对应工作进程,和旧版本一起处理请求;
6.发送WINCH信号:向旧的nginx主进程(master)发送WINCH信号,他会逐步关闭自己的工作进程(主进程不退出),这时所有请求都会由新版本nginx处理;
7.发送QUIT信号:升级完毕,可向旧的nginx主进程(master)发送(QUIT、TERM或者KILL)信号,使旧的主进程退出;
8.验证nginx版本号,并访问测试;
配置
查看旧版nginx的编译参数
[root@localhost ~]# /data/nginx/sbin/nginx -V
nginx version: nginx/1.10.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/data/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
安装新版本nginx
[root@localhost ~]# tar xf nginx-1.12.2.tar.gz -C /data/
[root@localhost ~]# cd /data/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure --prefix=/data/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
注:##不能加make install,如若添加,则覆盖了
备份旧版本nginx二进制文件
[root@localhost nginx-1.12.2]# mv /data/nginx/sbin/nginx /data/nginx/sbin/nginx.old
[root@localhost nginx-1.12.2]# cp objs/nginx /data/nginx/sbin/nginx
[root@localhost nginx-1.12.2]# /data/nginx/sbin/nginx -t
nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx/conf/nginx.conf test is successful
使用信号实现热升级
[root@localhost ~]# ps aux |grep nginx |grep -v grep
root 3496(旧版本主进程) 0.0 0.2 45000 1128 ? Ss 01:13 0:00 nginx: master process nginx
nginx 3497(旧版本从进程) 0.0 0.4 45432 2004 ? S 01:13 0:00 nginx: worker process
[root@localhost ~]# kill -USR2 3496
##发送 USR2 信号:向旧版本主进程( master)发送 USR2 信号, nginx 会启动一个新版本的 master 进程和对应工作进程,和旧版一起处理请求。
[root@localhost ~]# ps aux |grep nginx |grep -v grep
root 3496(旧版本主进程) 0.0 0.2 45000 1296 ? Ss 02:39 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 3497(旧版本从进程) 0.0 0.3 45432 1724 ? S 02:39 0:00 nginx: worker process
root 6041(新版本主进程) 0.0 0.6 45184 3144 ? S 02:41 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 6042(新版本从进程) 0.0 0.3 45636 1756 ? S 02:41 0:00 nginx: worker process
[root@localhost ~]# kill -WINCH 3496 ##向旧版本主进程发送WINCH信号,用来关闭旧版本的worker进程
[root@localhost ~]# ps aux |grep nginx |grep -v grep
root 3496(旧版本主进程) 0.0 0.2 45000 1300 ? Ss 02:39 0:00 nginx: master process /usr/local/nginx/sbin/nginx
root 6041(新版本主进程) 0.0 0.6 45184 3144 ? S 02:41 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 6042(新版本从进程) 0.0 0.3 45636 1756 ? S 02:41 0:00 nginx: worker process
[root@localhost ~]# kill -QUIT 3496 ##关闭旧版本的master进程
[root@localhost ~]# ps aux |grep nginx |grep -v grep
root 6041(新版本主进程) 0.0 0.6 45184 3144 ? S 02:41 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 6042(新版本从进程) 0.0 0.3 45636 1756 ? S 02:41 0:00 nginx: worker process
[root@localhost ~]# /data/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/data/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
验证