Nginx的启动关闭和重启
nginx可以通过信号指令控制进程,常用的信号有:
QUIT:处理完当前请求后关闭进程
HUP:重新加载配置,也就是关闭原有的进程,开启新的进程,此操作不会中断用户的访问请求,所以又称为平滑重启nginx
USR1:用于nginx的日志切换,也就是重新打开一个日志文件,每天要生成一个新的日志文件时,可以使用该信号控制。
USR2:平滑升级可执行程序
WINCH :从容关闭工作进程
常用的方法控制有:
① kill –XXX主进程号
[root@localhost run]# ps -ef |grep nginx
root 2771 1 0 10:34 ? 00:00:00 nginx: master process/opt/nginx/sbin/nginx
503 2772 2771 0 10:34 ? 00:00:00 nginx: worker process
503 2773 2771 0 10:34 ? 00:00:00 nginx: worker process
503 2774 2771 0 10:34 ? 00:00:00 nginx: worker process
503 2775 2771 0 10:34 ? 00:00:00 nginx: worker process
root 3575 2730 0 12:08 pts/1 00:00:00 grep nginx
[root@localhost run]# kill -QUIT2771
[root@localhost run]# ps -ef |grep nginx
root 3578 2730 0 12:09 pts/1 00:00:00 grep nginx
[root@localhost run]#/opt/nginx/sbin/nginx
[root@localhost run]# ps -ef |grep nginx
root 3580 1 0 12:09 ? 00:00:00 nginx: master process/opt/nginx/sbin/nginx
503 3581 3580 0 12:09 ? 00:00:00 nginx: worker process
503 3582 3580 0 12:09 ? 00:00:00 nginx: worker process
503 3583 3580 0 12:09 ? 00:00:00 nginx: worker process
503 3584 3580 0 12:09 ? 00:00:00 nginx: worker process
root 3588 2730 0 12:10 pts/1 00:00:00 grep nginx
[root@localhost run]# kill -HUP3580
[root@localhost run]# ps -ef |grep nginx
root 3580 1 0 12:09 ? 00:00:00 nginx: master process/opt/nginx/sbin/nginx
503 3590 3580 0 12:10 ? 00:00:00 nginx: worker process
503 3591 3580 0 12:10 ? 00:00:00 nginx: worker process
503 3592 3580 0 12:10 ? 00:00:00 nginx: worker process
503 3593 3580 0 12:10 ? 00:00:00 nginx: worker process
root 3596 2730 0 12:10 pts/1 00:00:00 grep nginx
[root@localhost run]# kill -WINCH3580
[root@localhost run]# ps -ef |grep nginx
root 3580 1 0 12:09 ? 00:00:00 nginx: master process/opt/nginx/sbin/nginx
② kill -信号类型`cat /安装目录/logs/nginx.pid`
使用该方法时需要在nginx.conf配置了pid文件存放路径则该文件存放的就是Nginx主进程号,如果没指定则放在nginx的logs目录下。有了pid文件,我们就不用先查询Nginx的主进程号,而直接向Nginx发送信号了,命令如下:
kill -信号类型`cat /安装目录/logs/nginx.pid` ,本人将nginx安装于/opt/nginx目录下
[root@localhost conf]# cd /opt/nginx/logs/
[root@localhost logs]# ls
access.log error.log nginx.pid
[root@localhost logs]# ps -ef |grep nginx
root 3580 1 0 12:09 ? 00:00:00 nginx: master process/opt/nginx/sbin/nginx
503 3680 3580 0 12:28 ? 00:00:00 nginx: worker process
503 3681 3580 0 12:28 ? 00:00:00 nginx: worker process
503 3682 3580 0 12:28 ? 00:00:00 nginx: worker process
503 3683 3580 0 12:28 ? 00:00:00 nginx: worker process
root 3687 2730 0 12:28 pts/1 00:00:00 grep nginx
[root@localhost logs]# kill -HUP`cat /opt/nginx/logs/nginx.pid`
[root@localhost logs]# ps -ef |grep nginx
root 3580 1 0 12:09 ? 00:00:00 nginx: master process/opt/nginx/sbin/nginx
503 3689 3580 0 12:28 ? 00:00:00 nginx: worker process
503 3690 3580 0 12:28 ? 00:00:00 nginx: worker process
503 3691 3580 0 12:28 ? 00:00:00 nginx: worker process
503 3692 3580 0 12:28 ? 00:00:00 nginx: worker process
root 3694 2730 0 12:28 pts/1 00:00:00 grep nginx
[root@localhost logs]#