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

用上面编译参数

./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)这时候我们可以决定是使用新的版本还是恢复到旧版本。

Nginx 启动、停止、平滑重启和平滑升级 graceful shutdown of worker processes的更多相关文章

  1. nginx平滑重启与平滑升级的方法

    如何实现nginx平滑重启与平滑升级? 平滑重启 kill -HUP `cat /usr/local/www/nginx/logs/nginx.pid` 平滑升级nginx: cd /yujialin ...

  2. Nginx的平滑重启和平滑升级

    一,Nginx的平滑重启如果改变了Nginx的配置文件(nginx.conf),想重启Nginx,可以发送系统信号给Nginx主进程的方式来进行.在重启之前,要确认Nginx配置文件的语法是正确的. ...

  3. nginx 启动/停止/重启

    启动: -c filename   : set configuration file (default: conf/nginx.conf) [root@LinuxServer sbin]# /usr/ ...

  4. nginx 启动/停止/重启 BAT

    cls @ECHO OFF SET NGINX_PATH=D: SET NGINX_DIR=D:\Hack\nginx\color 0a TITLE Nginx 管理程序 Power By AntsG ...

  5. Nginx启动停止命令

    操作环境是Windows 一.nginx命令:启动nginx 在Windows上安装好nginx后,我们需要启动nginx服务,启动nginx服务的命令行操作主要有两种方式,即 cd D:\opens ...

  6. ubuntu14.04上 nginx启动停止

    sudo service nginx stop  停止 sudo nginx   启动

  7. Nginx启动关闭和重启、文档直接下载不阅览

    nginx启动相关 启动:sbin/nginx -c conf/nginx.conf 关闭:sbin/nginx -s stop 重启(重新加载配置文件):sbin/nginx -s reload 检 ...

  8. nginx启动停止

    nginx -s reload :修改配置后重新加载生效 nginx -s reopen :重新打开日志文件 nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否 ...

  9. Nginx学习——Nginx启动、停止、重启和信号控制以及平滑升级

    1.Nginx 启动与停止 (1)启动方式 启动格式:Nginx可执行文件地址 -c Nginx配置文件地址 /etc/local/nginx/sbin/nginx -c /root/dufy/ngi ...

随机推荐

  1. Android短信发送器(2)

    在上一篇的<Android短信发送器>当中.发送功能并不完好.当发送内容超过限定字数时,短信就会发送失败,此时就须要推断内容是否超过限制,假设不超过限制.就直接发送,反之.则对其进行处理再 ...

  2. 二维数组,锯齿数组和集合 C# 一维数组、二维数组(矩形数组)、交错数组(锯齿数组)的使用 C# 数组、多维数组(矩形数组)、锯齿数组(交叉数组)

    二维数组,锯齿数组和集合 一.二维数组 二维数组:一维数组----豆角二维数组----表格 定义:1.一维数组:数据类型[] 数组变量名 = new 数据类型[数组长度];数据类型[] 数组变量名 = ...

  3. MaterialUp 官方client源代码

    Material Design MaterialUp 官方client源代码  https://github.com/jariz/MaterialUp

  4. win8 应用商店。 app下载的音乐和视频软件能打开,不能正常播放 解决方法

    win8 应用商店.app下载的音乐和视频软件能打开,不能正常播放 安装完win8之后,下载了PPS,可以正常播放.但是过了几天之后,就不能播放了,又从网络上下载了其他的音乐和视频相关的软件, 都不可 ...

  5. android launcher2开发之 有抽屉改成无抽屉

    在launcher.java中在createShortcut方法中   屏蔽全部应用button 修改之前 View createShortcut(int layoutResId, ViewGroup ...

  6. Shift Register(Using Submodule)

    /*************************************************** /  Shift Register module by Submodule /  Progra ...

  7. 红茶一杯话Binder (传输机制篇_中)

    红茶一杯话Binder (传输机制篇_中) 侯 亮 1 谈谈底层IPC机制吧 在上一篇文章的最后,我们说到BpBinder将数据发到了Binder驱动.然而在驱动层,这部分数据又是如何传递到BBind ...

  8. __attribute__系列之cleanup

    cleanup属性:当变量离开它的作用域时,设置的cleanup_function函数将被调用. cleanup (cleanup_function) The cleanup attribute ru ...

  9. iOS valueForKeyPath快速计算求和、平均值、最大、最小

    iOS中开始取出数组中最大值,最小值除了使用排序的方式,还可以使用valueForKeyPath的方式直接取出 array = @[@(10),@(100),@(20),@(97)]; CGFloat ...

  10. FreeBSD长模式不兼容

    二进制转换与此平台上的长模式不兼容.此虚拟环境中的长模式将被禁用.因此需要使用长模式的应用程序将无法正常运行.请参见 http://vmware.com/info?id=152 了解更多详细信息. m ...