Nginx 启动、停止、平滑重启和平滑升级 graceful shutdown of worker processes

Stella981
• 阅读 861

Controlling nginx http://nginx.org/en/docs/control.html

nginx can be controlled with signals. The process ID of the master process is written to the file/usr/local/nginx/logs/nginx.pid by default. This name may be changed at configuration time, or innginx.conf using the pid directive. The master process supports the following signals:

TERM, INT

fast shutdown

QUIT

graceful shutdown

HUP

changing configuration, keeping up with a changed time zone (only for FreeBSD and Linux), starting new worker processes with a new configuration, graceful shutdown of old worker processes

USR1

re-opening log files

USR2

upgrading an executable file

WINCH

graceful shutdown of worker processes

Individual worker processes can be controlled with signals as well, though it is not required. The supported signals are:

TERM, INT

fast shutdown

QUIT

graceful shutdown

USR1

re-opening log files

WINCH

abnormal termination for debugging (requires debug_points to be enabled)

Changing Configuration

In order for nginx to re-read the configuration file, a HUP signal should be sent to the master process. The master process first checks the syntax validity, then tries to apply new configuration, that is, to open log files and new listen sockets. If this fails, it rolls back changes and continues to work with old configuration. If this succeeds, it starts new worker processes, and sends messages to old worker processes requesting them to shut down gracefully. Old worker processes close listen sockets and continue to service old clients. After all clients are serviced, old worker processes are shut down.

Let’s illustrate this by example. Imagine that nginx is run on FreeBSD and the command

ps axw -o pid,ppid,user,%cpu,vsz,wchan,command | egrep '(nginx|PID)'

produces the following output:

PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
33126     1 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx
33127 33126 nobody   0.0  1380 kqread nginx: worker process (nginx)
33128 33126 nobody   0.0  1364 kqread nginx: worker process (nginx)
33129 33126 nobody   0.0  1364 kqread nginx: worker process (nginx)

If HUP is sent to the master process, the output becomes:

PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx
33129 33126 nobody   0.0  1380 kqread nginx: worker process is shutting down (nginx)
33134 33126 nobody   0.0  1368 kqread nginx: worker process (nginx)
33135 33126 nobody   0.0  1368 kqread nginx: worker process (nginx)
33136 33126 nobody   0.0  1368 kqread nginx: worker process (nginx)

One of the old worker processes with PID 33129 still continues to work. After some time it exits:

PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx
33134 33126 nobody   0.0  1368 kqread nginx: worker process (nginx)
33135 33126 nobody   0.0  1368 kqread nginx: worker process (nginx)
33136 33126 nobody   0.0  1368 kqread nginx: worker process (nginx)

Rotating Log-files

In order to rotate log files, they need to be renamed first. After that USR1 signal should be sent to the master process. The master process will then re-open all currently open log files and assign them an unprivileged user under which the worker processes are running, as an owner. After successful re-opening, the master process closes all open files and sends the message to worker process to ask them to re-open files. Worker processes also open new files and close old files right away. As a result, old files are almost immediately available for post processing, such as compression.

Upgrading Executable on the Fly

In order to upgrade the server executable, the new executable file should be put in place of an old file first. After that USR2 signal should be sent to the master process. The master process first renames its file with the process ID to a new file with the .oldbin suffix, e.g./usr/local/nginx/logs/nginx.pid.oldbin, then starts a new executable file that in turn starts new worker processes:

PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx
33134 33126 nobody   0.0  1368 kqread nginx: worker process (nginx)
33135 33126 nobody   0.0  1380 kqread nginx: worker process (nginx)
33136 33126 nobody   0.0  1368 kqread nginx: worker process (nginx)
36264 33126 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx
36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

After that all worker processes (old and new ones) continue to accept requests. If the WINCH signal is sent to the first master process, it will send messages to its worker processes, requesting them to shut down gracefully, and they will start to exit:

PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx
33135 33126 nobody   0.0  1380 kqread nginx: worker process is shutting down (nginx)
36264 33126 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx
36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

After some time, only the new worker processes will process requests:

PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
33126     1 root     0.0  1164 pause  nginx: master process /usr/local/nginx/sbin/nginx
36264 33126 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx
36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

It should be noted that the old master process does not close its listen sockets, and it can be managed to start its worker processes again if needed. If for some reason the new executable file works unacceptably, one of the following can be done:

  • Send the HUP signal to the old master process. The old master process will start new worker processes without re-reading the configuration. After that, all new processes can be shut down gracefully, by sending the QUIT signal to the new master process.

  • Send the TERM signal to the new master process. It will then send a message to its worker processes requesting them to exit immediately, and they will all exit almost immediately. (If new processes do not exit for some reason, the KILL signal should be sent to them to force them to exit.) When the new master process exits, the old master process will start new worker processes automatically.

If the new master process exits then the old master process discards the .oldbin suffix from the file name with the process ID.

If upgrade was successful, then the QUIT signal should be sent to the old master process, and only new processes will stay:

PID  PPID USER    %CPU   VSZ WCHAN  COMMAND
36264     1 root     0.0  1148 pause  nginx: master process /usr/local/nginx/sbin/nginx
36265 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36266 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)
36267 36264 nobody   0.0  1364 kqread nginx: worker process (nginx)

Nginx 启动、停止、平滑重启和平滑升级 - 综合编程类其他综合 - 红黑联盟 https://www.2cto.com/kf/201701/586973.html

启动操作

# nginx -c /usr/local/nginx/conf/nginx.conf

-c参数指定了要加载的nginx配置文件路径

停止操作
停止操作是通过向nginx进程发送信号(什么是信号请参阅Linux文章)来进行的
步骤1:查询nginx主进程号

# ps -ef | grep nginx

在进程列表里面找master进程,它的编号就是主进程号了。
步骤2:发送信号
从容停止Nginx:

# kill-QUIT主进程号 快速停止Nginx:
# kill-TERM主进程号 强制停止Nginx:

# pkill-9主进程号 另外,若在nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginx的logs目录下。有了pid文件,我们就不用先查询Nginx的主进程号,而直接向Nginx发送信号了,命令如下:

# kill-信号类型'/usr/local/nginx/logs/nginx.pid'

平滑重启
如果更改了配置就要重启Nginx,要先关闭Nginx再打开?不是的,可以向Nginx发送信号,平滑重启。
平滑重启命令:

# kill-HUP住进称号或进程号文件路径

或者使用

# /usr/local/nginx/sbin/nginx-sreload

注意,修改了配置文件后最好先检查一下修改过的配置文件是否正确,以免重启后Nginx出现错误影响服务器稳定运行。判断Nginx配置是否正确命令如下:

# nginx-t-c/usr/local/nginx/conf/nginx.conf

或者

# /usr/local/nginx/sbin/nginx-t

当nginx接收到HUP信号时,它会尝试先解析配置文件(如果指定文件,就使用指定的,否则使用默认的),如果成功,就应用新的配置文件(例如:重新打开日志文件或监听的套接字),之后,nginx运行新的工作进程并从容关闭旧的工作进程,通知工作进程关闭监听套接字,但是继续为当前连接的客户提供服务,所有客户端的服务完成后,旧的工作进程就关闭,如果新的配置文件应用失败,nginx再继续使用早的配置进行工作。

补充内容:nginx的几种信号

TERM,INT 快速关闭

QUIT 从容关闭

HUP 平滑重启,重新加载配置文件

USR1 重新打开日志文件,在切割日志时用途较大

USR2 平滑升级可执行程序

WINCH 从容关闭工作进程

平滑升级

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

平滑升级命令:

cd /mnt

下载nginx升级包

wget http://nginx.org/download/_nginx_-1.10.2.tar.gz

解压升级包

tar zxvf nginx-1.10.2.tar.gz

cd nginx-1.10.2/

查看当前版本得到编译参数

/usr/local/nginx/sbin/nginx -V

Nginx 启动、停止、平滑重启和平滑升级   graceful shutdown of worker processes

用上面编译参数

./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-ipv6 --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-openssl=../openssl-1.0.2j --with-pcre=../pcre-8.39 --with-pcre-jit --with-ld-opt='-ljemalloc'

然后make,千万别make install

make完了 在objs目录下就多了个nginx,这个就是新版本的程序了

备份原nginx文件

mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx-2017110

将新生成nginx执行文件复制到nginx/sbin下

cp objs/nginx /usr/local/nginx/sbin/nginx

检测配置文件是否正确

/usr/local/nginx/sbin/nginx -t

执行升级

make upgrade

执行完后

/usr/local/nginx/sbin/nginx -V

到此就完成平滑升级。

Nginx的启动、停止、平滑启动、平滑升级_服务器应用_Linux公社-Linux系统门户网站 https://www.linuxidc.com/Linux/2015-08/121856.htm

Nginx的启动
启动nginx,可以执行一下命令(默认安装位置): 
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf 
参数“-c”指定了配置文件的路径,如果不加,则Nginx会默认加载其安装目录的conf子目录中的ngin.conf

Nginx的停止
nginx的停止方法有很多种,一般是发送系统信号给nginx主进程来停止nginx。 
我们通过ps命令来查找nginx的主进程号

ps -ef |grep nginx
我们可以看到备注信息为“master process”它表示主进程。为“worker”的是子进程。

如果在nginx.conf中指定了pid文件存放路径,则该文件存放的就是nginx的主进程号。如果没有指定,则默认存放在Nginx安装目录的log目录下。所以我们还可以这样做:

kill -信号类型 '/usr/local/nginx/logs/nginx.pid'
nginx支持以下几种信号: 
TERM,INT :快速关闭 
QUIT:从容关闭 
HUP:平滑启动 
USR1:重新打开日志文件 
USR2:平滑升级可执行程序 
EINCH:从容关闭工作进程 
(1)从容停止nginx

    kill -QUIT Nginx 主进程号
(2)快速停止Nginx

kill -TERM Nginx主进程号
(3)强制停止所有nginx进程

pkill -9 nginx
Nginx的平滑启动
kill -HUP nginx主进程号

nginx平滑升级
当需要将正在运行的nginx升级、添加/删除服务器模块时,可以在不中断的情况下使用新版本、重编译的nginx可执行程序替换旧版本的可执行程序。步骤如下: 
(1)备份旧的可执行程序 
(2)发送以下指令

kill -USR2 旧的版本nginx主进程号
(3)旧版本的nginx的主进程将重命名他的pid文件为.oldbin。然后执行新版本的nginx可执行程序。依次启动新的主进程和新的工作进程。 
(4)此时新旧版本的nginx会同时运行,共同处理输入请求。要逐步停止旧版本的nginx实例,需要发送WINCH信号给旧的主进程,然后他的工作进程就从容关闭:

kill -WINCH 旧版本的主进程号
(5)一段时间后,旧的工作进程处理完所有的已连接请求后退出,仅有新的工作进程来处理输入请求。 
(6)这时候我们可以决定是使用新的版本还是恢复到旧版本。

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
3年前
4. Nginx模块
Nginx官方模块1.ngx\_http\_stub\_status\_modulehttp://nginx.org/en/docs/http/ngx\_http\_stub\_status\_module.html。(https://www.oschina.net/action/GoToLink?urlhttp%3A%2
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
10个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这