Nginx平滑升级和平滑重启

        若是要对当前的Nginx服务器进行版本升级,应用新模块,若是用户访问量比较大的时候,若是须要在不影响客户的状况下进行升级的话,这时候就得考虑平滑升级了。node

        平滑升级的过程,Nginx服务器接受到USR2信号后,首先将旧的nginx.pid文件添加后缀.oldbin,变为nginx.pid.oldbin文件,而后执行新版本的Nginx服务器的二进制的文件启动服务,这个时候须要提早将编译好的新版本的二进制实现复制到sbin文件夹中。若是新的服务启动成功,系统中将有新旧两个Nginx服务共同提供Web服务。以后,须要向旧的Nginx服务器进程发送WINCH信号,使旧的Nginx服务平滑中止,并删除nginx.pid.oldbin文件。在发送WINCH信号以前,若是发现有什么错误,能够随时中止新的Nginx服务。nginx


        这里以前已经编译安装了naginx1.8.0。编译的时候只指定了按照路径 --prefix=/nginx/。这里演示从nginx1.8.0升级到nginx 1.9.6
web

[root@c7node1 ~]# /nginx/sbin/nginx -V
nginx version: nginx/1.8.0
built by gcc 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) 
configure arguments: --prefix=/nginx/
[root@c7node1 ~]# ss -tnl
LISTEN     0      12                 *:80                             *:*


        这里为了测试升级过程当中Web没有中断,提供一个脚本让其每秒去请求页面服务。并纪录到日志中vim

[root@c7node1 test]# vim http.sh 
#!/bin/bash
#
URL="http://192.168.0.66/"
HTTP_CODE=`curl -o /dev/null -s -w "%{http_code}" "${URL}"`
if [ $HTTP_CODE == 200 ];then
    echo Success.$HTTP_CODE >> /tmp/ping.http
else
    echo Failure.404 >> /tmp/ping.http
fi
#请求http服务,把请求成功和失败发送到ping.http文件中

[root@c7node1 test]# vim cron.sh 
#!/bin/bash
#
while true;do
/test/http.sh
sleep 1
done
#为cron服务提供每秒执行服务

[root@c7node1 test]# crontab -e
* * * * * /bin/bash /test/cron.sh
#定义每分钟执行cron.sh任务



        下载编译nginx-1.9.6,并进行平滑升级,为了实现Nginx服务器的平滑升级,新的服务安装路径应该和旧的服务安装路径保持一致。若是由于某种缘由不一致,须要把旧的服务的安装路径更改成新的服务的安装路径,修改方法为(nginx -p NewInstallPath)bash

[root@c7node1 ~]# wget http://nginx.org/download/nginx-1.9.6.tar.gz
[root@c7node1 ~]# tar xf nginx-1.9.6.tar.gz
[root@c7node1 ~]# cd nginx-1.9.6
[root@c7node1 nginx-1.9.6]# ./configure --prefix=/nginx/ 
[root@c7node1 nginx-1.9.6]# make
[root@c7node1 mv /nginx/sbin/nginx{,.bak}
#这里备份旧的nginx,一方面是等一下须要把新的nginx复制过来,另外一方面的若是新的nginx有问题,还能够进行恢复
[root@c7node1 nginx-1.9.6]# cp objs/nginx /nginx/sbin/
#把编译好的新的nginx文件服务到指定的安装目录中
[root@c7node1 sbin]# /nginx/sbin/nginx -t
nginx: the configuration file /nginx//conf/nginx.conf syntax is ok
nginx: configuration file /nginx//conf/nginx.conf test is successful
#测试新编译的nginx检测配置文件是否正常
[root@c7node1 nginx-1.9.6]# kill -USR2 `cat /nginx/logs/nginx.pid`
#使用新版本的Nginx文件启动服务,以后平缓中止原有Nginx进程
[root@c7node1 nginx-1.9.6]# cd /nginx/logs/
access.log  error.log  nginx.pid  nginx.pid.oldbin
#能够看到这里自动把以前的nginx.pid命名为nginx.pid.oldbin,并为新的nginx生成nginx.pid
[root@c7node1 logs]# kill -WINCH `cat /nginx/logs/nginx.pid.oldbin`
#平缓中止旧服务的worker process
[root@c7node1 logs]# nginx -V
nginx version: nginx/1.9.6
built by gcc 4.8.3 20140911 (Red Hat 4.8.3-9) (GCC) 
configure arguments: --prefix=/nginx/

[root@c7node1 logs]# ps -aux |grep nginx
root       990  0.0  0.2  24316  1880 ?        S    11:25   0:00 nginx: master process nginx
nobody     992  0.9  0.2  24724  1712 ?        S    11:25   0:03 nginx: worker process
root      2357  0.0  0.1  24308   896 ?        Ss   11:20   0:00 nginx: master process nginx
#能够看到有两个master process进程



测试结果服务器

[root@c7node1 ~]# sort /tmp/ping.http | uniq -c
  32822 Success.200
#能够看到服务并无停止



        此部份内容参考《Nginx 高性能web服务器详解》    网络

快速中止Nginx服务:快速中止是指当即中止当前Nginx服务正在处理的全部网络请求,立刻丢弃链接,中止工做curl

[root@c7node1 ~]# kill -TERM `cat /nginx/logs/nginx.pid`
[root@c7node1 ~]# kill -INT `cat /nginx/logs/nginx.pid`


平缓中止Nginx服务:平缓中止是指容许Nginx服务将当前正在处理的网络请求处理完成,但再也不接受新的请求,以后关闭链接,中止工做ide

[root@c7node1 ~]# kill -QUIT `cat /nginx/logs/nginx.pid`


平缓重启Nginx服务:Nginx服务进程接受到信号后,首先读取新的Nginx配置文件,若是配置语法正确,则启动新的Nginx服务,而后平缓关闭旧的服务进程,若是新的Nginx配置文件有问题,将显示错误,仍然使用旧的Nginx进程提供服务性能

[root@c7node1 ~]# kill -HUP `cat /nginx/logs/nginx.pid`


日志切割:从新打开日志文件,经常使用于日志切割

[root@c7node1 ~]# kill -USR1 `cat /nginx/logs/nginx.pid`


平缓升级Nginx服务:使用新版本的Nginx文件启动服务,以后平缓中止原有的Nginx进程

[root@c7node1 ~]# kill -USR2 `cat /nginx/logs/nginx.pid`


平缓中止worker process:用于Nginx服务平缓升级

[root@c7node1 ~]# kill -WINCH `cat /nginx/logs/nginx.pid`
相关文章
相关标签/搜索