转发:https://blog.csdn.net/gnail_oug/article/details/52754491
Nginx有一个主进程和几个工作进程,主进程的主要作用就是读取、评估配置文件和管理工作进程,工作进程对请求做实际处理。工作进程的数量是在配置文件中配置的,一般设置为cpu的核心数*线程数。
nginx可以通过信号控制,默认情况下,主进程的进程ID记录在/usr/local/nginx/logs/nginx.pid 文件中。信号可以通过nginx可执行文件发送给nginx (nginx -s 信号),也可以通过unix的kill命令发送给nginx,这种情况下是将信号直接发送给指定进程ID的进程,如:kill QUIT 1628。
nginx的安装参考:CentOS6.5编译安装Nginx的方法
1、启动nginx,直接运行nginx的可执行文件:
[root@localhost ~]# /usr/local/nginx/sbin/nginx
2、停止nginx
快速关闭:nginx -s stop 或者kill -TERM 主进程号 或者kill -INT 主进程号
-
[root@localhost sbin]
# pwd
-
/usr/
local/nginx/sbin
-
[root@localhost sbin]
# ./nginx -s stop
或者
-
[root@localhost sbin]
# ps -ef |grep nginx|grep master
-
root
9879
1
0
13:
15 ?
00:
00:
00 nginx: master process ./nginx
-
[root@localhost sbin]
# kill -TERM 9879
或者
[root@localhost sbin]# kill -INT 9879
优雅关闭(不接受新的连接请求,等待旧的连接请求处理完毕再关闭):nginx -s quit 或者 kill -QUIT 主进程号
-
[root@songguoliang sbin]
# pwd
-
/usr/
local/nginx/sbin
-
[root@songguoliang sbin]
# ./nginx -s quit
注意:执行该命令的用户应该是启动nginx的用户
-
[root@localhost sbin]
# ps -ef |grep nginx
-
root
9889
1
0
13:
17 ?
00:
00:
00 nginx: master process ./nginx
-
nobody
9890
9889
0
13:
17 ?
00:
00:
00 nginx: worker process
-
root
9893
2008
0
13:
18 pts/
1
00:
00:
00
grep nginx
-
[root@localhost sbin]
# kill -QUIT 9889
3、重新加载配置文件:nginx -s reload 或者 kill -HUP 主进程号
nginx配置平滑更新
为了让主进程重新读取配置文件,应该向主进程发送一个HUP信号,主进程一旦接收到重新加载配置的的信号,它就检查配置文件语法的有效性,然后试图应用新的配置,即打开新的日志文件和新的socket 监听,如果失败,它将回滚配置更改并继续使用旧的配置,如果成功了,它开启新的工作进程,并给旧的工作进程发消息让它们优雅的关闭,旧的工作进程接收到关闭信号后,不再接收新的请求,如果已有请求正在处理,等当前请求处理完毕后关闭,如果没有请求正在处理,则直接关闭。
[root@localhost sbin]# ./nginx -s reload
或者
-
[root@localhost sbin]
# ps -ef|grep nginx
-
root
9944
1
0
13:
22 ?
00:
00:
00 nginx: master process ./nginx
-
nobody
9949
9944
0
13:
23 ?
00:
00:
00 nginx: worker process
-
root
9960
9917
0
13:
28 pts/
1
00:
00:
00
grep nginx
-
[root@songguoliang sbin]
# kill -HUP 9944
4、测试配置文件,检查配置文件语法是否正确,然后试图打开文件涉及的配置:nginx -t
-
[root@localhost sbin]# ./nginx -t
-
nginx: the configuration
file /usr/
local/nginx/conf/nginx.conf syntax
is ok
-
nginx: configuration
file /usr/
local/nginx/conf/nginx.conf test
is successful
5、查看nginx版本信息: nginx -v
-
[root@songguoliang sbin]
# ./nginx -v
-
nginx version: nginx/
1.10.
1
6、查看nginx版本信息,编译版本,和配置参数:nginx -V
-
[
root@songguoliang sbin]
# ./nginx -V
-
nginx version: nginx/
1.10
.1
-
built
by gcc
4.4
.7
20120313 (Red Hat
4.4
.7
-17) (GCC)
-
configure arguments: --prefix=/usr/local/nginx
[root@localhost sbin]# ./nginx -s reopen
或者
[root@localhost sbin]# kill -USR1 2030
由于nginx是通过inode指向日志文件的,inode和文件名无关,所以即使把日志文件重命名,nginx还是将日志文件写入原文件,只有用上面的命令重新开启日志文件才能将日志写入新的日志文件。
如果你需要升级或者添加、删除服务器模块时,可以通过nginx的平滑升级,在不停止服务的情况下升级nginx。
(1)用新的nginx可执行程序替换旧的可执行程序,即下载新的nginx,重新编译到旧版本的安装路径中(重新编译之前可以备份旧的可执行文件)
(2)给nginx主进程号发送USR2信号
[root@localhost sbin]# kill -USR2 9944
执行命令前的进程:
-
[root@localhost sbin]
# ps -ef |grep nginx
-
root
9944
1
0
13:
22 ?
00:
00:
00 nginx: master process ./nginx
-
nobody
9965
9944
0
13:
29 ?
00:
00:
00 nginx: worker process
-
root
10010
9917
0
13:
42 pts/
1
00:
00:
00
grep nginx
执行命令后的进程:
-
[root@localhost sbin]
# ps -ef |grep nginx
-
root
9944
1
0
13:
22 ?
00:
00:
00 nginx: master process ./nginx
-
nobody
9965
9944
0
13:
29 ?
00:
00:
00 nginx: worker process
-
root
10012
9944
0
13:
43 ?
00:
00:
00 nginx: master process ./nginx
-
nobody
10013
10012
0
13:
43 ?
00:
00:
00 nginx: worker process
-
root
10015
9917
0
13:
43 pts/
1
00:
00:
00
grep nginx
给nginx发送USR2信号后,nginx会将logs/nginx.pid文件重命名为nginx.pid.oldbin,然后用新的可执行文件启动一个新的nginx主进程和对应的工作进程,并新建一个新的nginx.pid保存新的主进程号
-
[root@songguoliang logs]
# pwd
-
/usr/
local/nginx/logs
-
[root@songguoliang logs]
# ll
-
总用量
16
-
-rw-r--r--.
1 nobody root
596
10月
8
13:
02 access.log
-
-rw-r--r--.
1 nobody root
881
10月
8
13:
43 error.log
-
-rw-r--r--.
1 root root
6
10月
8
13:
43 nginx.pid
-
-rw-r--r--.
1 root root
5
10月
8
13:
22 nginx.pid.oldbin
-
[root@songguoliang logs]
# cat nginx.pid
-
10012
-
[root@songguoliang logs]
# cat nginx.pid.oldbin
-
9944
这时,nginx的新的实例和旧的实例同时工作,共同处理请求连接。接下来要关闭旧的实例进程。
(3)给旧的主进程发送WINCH信号,kill -WINCH 旧的主进程号
-
[root@localhost sbin]
# ps -ef |grep nginx
-
root
9944
1
0
13:
22 ?
00:
00:
00 nginx: master process ./nginx
-
nobody
9965
9944
0
13:
29 ?
00:
00:
00 nginx: worker process
-
root
10012
9944
0
13:
43 ?
00:
00:
00 nginx: master process ./nginx
-
nobody
10013
10012
0
13:
43 ?
00:
00:
00 nginx: worker process
-
root
10092
9917
0
14:
05 pts/
1
00:
00:
00
grep nginx
-
[root@localhost sbin]
# kill -WINCH 9944
-
[root@localhost sbin]
#
-
[root@localhost sbin]
#
-
[root@localhost sbin]
#
-
[root@localhost sbin]
# ps -ef |grep nginx
-
root
9944
1
0
13:
22 ?
00:
00:
00 nginx: master process ./nginx
-
root
10012
9944
0
13:
43 ?
00:
00:
00 nginx: master process ./nginx
-
nobody
10013
10012
0
13:
43 ?
00:
00:
00 nginx: worker process
-
root
10094
9917
0
14:
06 pts/
1
00:
00:
00
grep nginx
旧的主进程号收到WINCH信号后,将旧进程号管理的旧的工作进程优雅的关闭。即一段时间后旧的工作进程全部关闭,只有新的工作进程在处理请求连接。这时,依然可以恢复到旧的进程服务,因为旧的进程的监听socket还未停止。
(4)给旧的主进程发送QUIT信号,使其关闭。
-
[root@localhost sbin]
# kill -QUIT 9944
-
[root@localhost sbin]
# ps -ef |grep nginx
-
root
10012
1
0
13:
43 ?
00:
00:
00 nginx: master process ./nginx
-
nobody
10013
10012
0
13:
43 ?
00:
00:
00 nginx: worker process
-
root
10118
9917
0
14:
16 pts/
1
00:
00:
00
grep nginx
给旧的主进程发送QUIT信号后,旧的主进程退出,并移除logs/nginx.pid.oldbin文件,nginx的升级完成。
中途停止升级,回滚到旧的nginx
在步骤(3)时,如果想回到旧的nginx不再升级
(1)给旧的主进程号发送HUP命令,此时nginx不重新读取配置文件的情况下重新启动旧主进程的工作进程。
[root@localhost sbin]# kill -HUP 9944
(2)优雅的关闭新的主进程
[root@localhost sbin]# kill -QUIT 10012
注:如果由于某种原因,无法关闭新主进程的工作进程,直接给进程号发送KILL信号
nginx的进程分为主进程和工作进程,关于nginx的信号,主进程支持以下信号:
TERM,INT 快速关闭
QUIT 优雅的关闭
KILL 关闭一个顽固进程
HUP 改变配置,使用新配置开启新的工作进程,优雅的关闭旧的进程
USR1 重新开启日志文件
USR2 平滑升级nginx
WINCH 优雅的关闭工作进程
单个工作进程也可以通过信号控制,但它不是必须的。单个工作进程支持的信号有:
TERM,INT 快速关闭
QUIT 优雅的关闭
USR1 重新开启日志文件
WINCH 异常终止调试(需要启动debug_points)