欢迎光临
我们一直在努力

nginx的平滑升级

$$$$$$$$$$$$$$$$$$$$$$$$$$$$理论大概$$$$$$$$$$$$$$$$$$$$$$$

一、为什么要对 nginx 平滑升级
随着 nginx 越来越流行,并且 nginx 的优势也越来越明显,nginx 的版本迭代也来时加速 模式,1.9.0版本的nginx更新了许多新功能,例如 stream 四层代理功能,伴随着 nginx 的广泛应用,版本升级必然越来越快,线上业务不能停,此时 nginx 的升级就是运维的工作了。

二、nginx 方便地帮助我们实现了平滑升级。其原理简单概括,就是:
(1)在不停掉老进程的情况下,启动新进程。
(2)老进程负责处理仍然没有处理完的请求,但不再接受处理请求。
(3)新进程接受新请求。
(4)老进程处理完所有请求,关闭所有连接后,停止。 这样就很方便地实现了平滑升级。一般有两种情况下需要升级 nginx,一种是确实要升级 nginx 的版本,另一种是要为 nginx 添加新的模块。

三、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: 重新打开日志文件

%%%%%%%%%%%%%%%%%%%nginx 平滑升级的过程%%%%%%%%%%%%%%%%%%

1、yum安装的nginx升级,以当前nginx为nginx-1.12.2版本升级到nginx-1.16.1为例

 yum -y install nginx          (为什么可以直接yum,因为epel扩展源里存在nginx-1.12.2)

systemctl start nginx
systemctl status nginx      (确定已经开启nginx)     

nginx -V                        检测版本与配置参数

nginx version: nginx/1.12.2
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/share/nginx –sbin-path=/usr/sbin/nginx –modules-path=/usr/lib64/nginx/modules –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –http-client-body-temp-path=/var/lib/nginx/tmp/client_body –http-proxy-temp-path=/var/lib/nginx/tmp/proxy –http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi –http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi –http-scgi-temp-path=/var/lib/nginx/tmp/scgi –pid-path=/run/nginx.pid –lock-path=/run/lock/subsys/nginx –user=nginx –group=nginx –with-file-aio –with-ipv6 –with-http_auth_request_module –with-http_ssl_module –with-http_v2_module –with-http_realip_module –with-http_addition_module –with-http_xslt_module=dynamic –with-http_image_filter_module=dynamic –with-http_geoip_module=dynamic –with-http_sub_module –with-http_dav_module –with-http_flv_module –with-http_mp4_module –with-http_gunzip_module –with-http_gzip_static_module –with-http_random_index_module –with-http_secure_link_module –with-http_degradation_module –with-http_slice_module –with-http_stub_status_module –with-http_perl_module=dynamic –with-mail=dynamic –with-mail_ssl_module –with-pcre –with-pcre-jit –with-stream=dynamic –with-stream_ssl_module –with-google_perftools_module –with-debug –with-cc-opt=’-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong –param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic’ –with-ld-opt=’-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E’

2,开始升级版本由nginx-1.12.2升级到nginx-1.16.1版本。按照原来的编译参数安装 nginx 的方法进行安装,只需要到 make,千万不要 make install 。如果make install 会将原来的配置文件覆盖

wget http://nginx.org/download/nginx-1.16.1.tar.gz     

tar xzf nginx-1.16.1.tar.gz -C /usr/local 解压压缩包到指定位置

yum -y install GeoIP GeoIP-devel GeoIP-data perl-devel perl-ExtUtils-Embed gd-devel libxml2 libxslt-devel gperftools

yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel 添加环境

cd /usr/local ( cd到解压后的目录 )
ls
cd nginx-1.16.1/

[root@localhost nginx-1.16.1]#nginx -V (配置参数前先查看原来版本的参数)
[root@localhost nginx-1.16.1]#./configure –prefix=/usr/share/nginx –sbin-path=/usr/sbin/nginx –modules-path=/usr/lib64/nginx/modules –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –http-client-body-temp-path=/var/lib/nginx/tmp/client_body –http-proxy-temp-path=/var/lib/nginx/tmp/proxy –http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi –http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi –http-scgi-temp-path=/var/lib/nginx/tmp/scgi –pid-path=/run/nginx.pid –lock-path=/run/lock/subsys/nginx –user=nginx –group=nginx –with-file-aio –with-ipv6 –with-http_auth_request_module –with-http_ssl_module –with-http_v2_module –with-http_realip_module –with-http_addition_module –with-http_xslt_module=dynamic –with-http_image_filter_module=dynamic –with-http_geoip_module=dynamic –with-http_sub_module –with-http_dav_module –with-http_flv_module –with-http_mp4_module –with-http_gunzip_module –with-http_gzip_static_module –with-http_random_index_module –with-http_secure_link_module –with-http_degradation_module –with-http_slice_module –with-http_stub_status_module –with-http_perl_module=dynamic –with-mail=dynamic –with-mail_ssl_module –with-pcre –with-pcre-jit –with-stream=dynamic –with-stream_ssl_module –with-google_perftools_module –with-debug
(配置升级版本的参数要与原版本相同的参数,复制到–with-debug 即可,其后的–with cc的都不需要)

make

3,备份原 nginx 二进制文件 (yum安装的nginx,其二进制文件和配置文件默认存放于/usr/sbin/)备份二进制文件和 nginx 的配置文件(期间nginx不会停止服务)
mv /usr/sbin/nginx /usr/sbin/nginx_$(date +%F)

4,复制新版本的nginx二进制文件,进入新的nginx源码包
cp /usr/local/nginx-1.16.1/objs/nginx /usr/sbin/

5,测试新版本的nginx是否正常
/usr/sbin/nginx -t
若出现如下报错:
nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so" version 1012002 instead of 1016001 in /usr/share/nginx/modules/mod-http-geoip.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
解决方法:
#vim /etc/nginx/nginx.conf (编辑nginx的主配置文件)
找到#include /usr/share/nginx/modules/*.conf;这一句并将其注释即可

/usr/sbin/nginx -t (再次测试便不会在报错)

6、给nginx发送平滑迁移信号(若不清楚pid路径,请查看/etc/nginx/nginx.conf配置文件)
kill -USR2 cat /var/run/nginx.pid

(查看pid也可find / -name "nginx.pid")

7.查看nginx pid,会出现一个nginx.pid.oldbin
[root@localhost nginx-1.16.1]# ll /var/run/nginx.pid*
-rw-r–r–. 1 root root 5 8月 24 01:30 /var/run/nginx.pid
-rw-r–r–. 1 root root 5 8月 24 01:30 /var/run/nginx.pid.oldbin

8、从容关闭旧的Nginx进程
#kill -WINCH cat /var/run/nginx.pid.oldbin

9、此时不重载配置启动旧的工作进程
#kill -HUP cat /var/run/nginx.pid.oldbin

10、结束工作进程,完成此次升级
#kill -QUIT cat /var/run/nginx.pid.oldbin

11、验证Nginx是否升级成功
nginx -V
nginx version: nginx/1.16.1 版本升级成功为nginx-1.16.1
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

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。