一、nginx的基本概念
Nginx 在启动后,会有一个 master 进程和多个 worker 进程。
master进程:接受外界的信号,向worker 发送信号,监控 worker 进程的运行状态,当 worker 进程退出后 (异常情况下),会自动重新启动新的 worker 进程
worker进程:接受客户端的请求,将请求一次送入各个功能模块进行过滤处理,与后端服务器通信,接收后端服务器处理结果,数据缓存 proxy_cache 模块,响应客户端请求
多个 worker 进程之间是对等的,他们同等竞争来自客户端的请求,各进程互相之间是独立的。一个请求,只可能在一个 worker 进程中处理,一个 worker 进程,不可能处理其它进程的请求。
二、nginx 信号简介
nginx 服务主进程能够接受的信号
1、TERM或INT:快速停止nginx服务
2、QUIT:平缓停止nginx 服务
3、HUP:使用新的配置文件启动进程,平缓停止原有进程,可以理解为“平滑重启”
4、USR1: 重新打开日志文件,常用户日志切割
5、USR2:使用新版本的nginx文件启动服务,之后平缓停止原有nginx进程,也就是所谓的“平滑升级”。
6、WINCH:平滑停止worker process,用于nginx服务器平滑升级。
三、升级
平滑升级过程:Nginx服务接收到USR2信号后,首先将旧的nginx.pid文件(如果在配置文件中更改过这个文件的名字,也是相同的过程)添加后缀.oldbin,变为nginx.pid.oldbin文件;然后执行新版本Nginx服务器的二进制文件启动服务.如果新的服务启动成功,系统中将有新旧两个Nginx服务共同提供Web服务.之后,需要向旧的Nginx服务进程发送WINCH信号,使旧的Nginx服务平滑停止,并删除nginx.pid.oldbin文件.在发送WINCH信号之前,可以随时停止新的Nginx 服务。
注意事项:
1、 新的服务器安装路径应该和旧的保持一致、在升级之前最后备份旧的nginx 。
2、安装之前在旧的nginx的sbin目录下执行./nginx -V 查看nginx 安装并启用了那些模块。
3、新的nginx 安装时 不执行 make install!!!
升级方法一:
[root@localhost ~]# cd /opt/nginx/sbin/
[root@localhost sbin]# ./nginx -V #查看启用了哪些模块
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt//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
[root@localhost opt]# mkdir 20220815
[root@localhost opt]# cp -r nginx 20220815/ #备份
[root@localhost opt]# wget http://nginx.org/download/nginx-1.22.0.tar.gz && tar -zxvf nginx-1.22.0.tar.gz #获取最新稳定版本的nginx
[root@localhost opt]# tar -zxvf nginx-1.22.0.tar.gz
[root@localhost opt]# cd nginx-1.22.0
[root@localhost nginx-1.22.0]# ./configure --prefix=/opt//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
[root@localhost nginx-1.22.0]# make
[root@localhost sbin]# cd /opt/nginx/sbin/ #将旧版本的nginx 备份重命名
[root@localhost sbin]# mv nginx nginx.bak
[root@localhost sbin]# cp /opt/nginx-1.22.0/objs/nginx ./
[root@localhost sbin]# kill -USR2 `cat /opt/nginx/logs/nginx.pid`
[root@localhost sbin]# ls ../logs/ #查看是否存在 nginx.pid.oldbin
access.log error.log nginx.pid nginx.pid.oldbin
[root@localhost sbin]#kill -QUIT `cat /opt/nginx/logs/nginx.pid.oldbin` #关闭旧的nginx
[root@localhost sbin]# ./nginx -V #再次查看升级成功
nginx version: nginx/1.22.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt//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
升级方法二:
[root@localhost ~]# cd /opt/nginx/sbin/
[root@localhost sbin]# ./nginx -V #查看启用了哪些模块
nginx version: nginx/1.20.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt//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
[root@localhost opt]# mkdir 20220815
[root@localhost opt]# cp -r nginx 20220815/ #备份
[root@localhost opt]# wget http://nginx.org/download/nginx-1.22.0.tar.gz && tar -zxvf nginx-1.22.0.tar.gz #获取最新稳定版本的nginx
[root@localhost opt]# tar -zxvf nginx-1.22.0.tar.gz
[root@localhost opt]# cd nginx-1.22.0
[root@localhost nginx-1.22.0]# ./configure --prefix=/opt//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
[root@localhost nginx-1.22.0]# make
[root@localhost sbin]# cd /opt/nginx/sbin/ #将旧版本的nginx 备份重命名
[root@localhost sbin]# mv nginx nginx.bak
[root@localhost nginx-1.22.0]# cd /opt/nginx-1.22.0/ #切换到新版nginx的安装目录
[root@localhost nginx-1.22.0]# make upgrade #执行后自动进行升级节约我们手敲几个命令仅此而已
/opt//nginx/sbin/nginx -t
nginx: the configuration file /opt//nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt//nginx/conf/nginx.conf test is successful
kill -USR2 `cat /opt//nginx/logs/nginx.pid`
sleep 1
test -f /opt//nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /opt//nginx/logs/nginx.pid.oldbin`
[root@localhost nginx-1.22.0]# cd /opt/nginx/sbin/
[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.22.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/opt//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