nginx信号控制

nginx的信号控制说明

nginx信号 含义
TERM,INT quick shutdown 一般用于快速的,紧急的杀掉进程,一般不建议使用
QUIT Gracefully shutdown 优雅的关闭进程,请求结束后再关闭进程
HUP Configuration reload ,Start the new worker processes with a new configuration Gracefully shutdown the old worker processes 用于改变了nginx的配置文件,不需要重启nginx,而让nginx平滑的重新取读取配置文件
USR1 repen the log files 重读日志,在日志切割时有用。 例如nginx的日志文件,写在access.log文件,文件需要进行切割,因为linux的文件的读写真正的指向并不是文件名,而是指向文件的inode,因此如果想要进行access.log文件的切割,即使是将access.log改名,然后重新创建一个access.log的文件,日志也不会往新的文件里面写,而继续写老的文件。这时候就需要用到USR1来进行切割。
USR2 Upgrade Executable on the fly 平滑的升级
WINCH Gracefully shutdown the worker processes 优雅的关闭旧的进程(配合USR2来进行升级的)

具体语法:
kill -信号选项 nginx的master进程号
或者
Kill -信号控制 cat /xxx/path/log/nginx.pid #本质上也是通过主进程号来控制

举例:

1、HUP信号例子

步骤一:在nginx.conf默认的工程目录/usr/share/nginx/html下面创建test.html文件,内容如下:

[root@docker html]# cat test.html 
<html>
Just test for nginx
</html>

步骤二:修改nginx.conf中server模块如下,在默认的index.html前面加上test.html:

test.html:
server {
        listen        80;
        server_name   localhost;
        location / {
           root    /usr/share/nginx/html;
           index   test.html index.html;
        }
        error_page  500  502  503  504  /50x.html;
        location = /50x.html{
           root  /usr/share/nginx/html;
        }
    }

步骤三:打开nginx的首页,确认目前的首页是nginx默认的欢迎页面:
nginx信号控制

步骤四:使用nginx的HUP信号控制,让nginx重读配置文件(注意:这不是重启nginx,查看kill -HUP前后,nginx的进程号都是没有改变的):

[root@docker nginx]# ps -ef|grep nginx
root      38951      1  0 23:10 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     39001  38951  0 23:11 ?        00:00:00 nginx: worker process
root      39685    957  0 23:25 pts/0    00:00:00 grep --color=auto nginx
[root@docker nginx]# 
[root@docker nginx]# kill -HUP 38951
[root@docker nginx]# 
[root@docker nginx]# ps -ef|grep nginx
root      38951      1  0 23:10 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     39698  38951  0 23:25 ?        00:00:00 nginx: worker process
root      39704    957  0 23:25 pts/0    00:00:00 grep --color=auto nginx
[root@docker nginx]# 

步骤五:刷新web界面,查看nginx首页已经变成了test.html页面的内容:
nginx信号控制

2、USR1使用举例

步骤一:进入/var/log/nginx目录,并刷新nginx界面,确认日志写入了access.log:

[root@docker nginx]# ll
总用量 8
-rw-r----- 1 nginx adm 2709 5月  20 23:27 access.log
-rw-r----- 1 nginx adm 2735 5月  20 23:25 error.log

刷新10.0.0.16这个界面,在查看access.log,日志在发生变化

[root@docker nginx]# ll
总用量 8
-rw-r----- 1 nginx adm 2899 5月  20 23:29 access.log
-rw-r----- 1 nginx adm 2735 5月  20 23:25 error.log
[root@docker nginx]# 

步骤二:手动将access.log改成access.log.bak,然后创建一个access.log,再刷新页面,查看日志,还是写往access.log.bak的:

[root@docker nginx]# cd /var/log/nginx/
[root@docker nginx]# mv access.log access.log.bak
[root@docker nginx]# touch access.log

[root@docker nginx]# ll
总用量 8
-rw-r--r-- 1 root  root    0 5月  20 23:39 access.log
-rw-r----- 1 nginx adm  2899 5月  20 23:29 access.log.bak
-rw-r----- 1 nginx adm  2735 5月  20 23:25 error.log
[root@docker nginx]# 

刷新10.0.0.16界面,再看日志还是在往老的access.log.bak文件写入,access.log文件依旧为空:

[root@docker nginx]# ll
总用量 8
-rw-r--r-- 1 root  root    0 5月  20 23:39 access.log
-rw-r----- 1 nginx adm  3849 5月  20 23:43 access.log.bak
-rw-r----- 1 nginx adm  2735 5月  20 23:25 error.log
[root@docker nginx]# 

步骤三:使用nginx的USR1信号切割日志(同样注意:这不是重启nginx,查看kill -HUP前后,nginx的进程号都是没有改变的):

[root@docker nginx]# ps -ef|grep nginx
root      38951      1  0 23:10 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     39698  38951  0 23:25 ?        00:00:00 nginx: worker process
root      40660    957  0 23:45 pts/0    00:00:00 grep --color=auto nginx
[root@docker nginx]# kill -USR1 38951
[root@docker nginx]# 
[root@docker nginx]# ps -ef|grep nginx
root      38951      1  0 23:10 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     39698  38951  0 23:25 ?        00:00:00 nginx: worker process
root      40672    957  0 23:45 pts/0    00:00:00 grep --color=auto nginx
[root@docker nginx]# 

步骤四:刷新web界面,再查看日志,就已经在往新的access.log文件写了:
刷新web界面之前的日志:

[root@docker nginx]# ll
总用量 8
-rw-r--r-- 1 root  root    0 5月  20 23:39 access.log
-rw-r----- 1 nginx adm  3849 5月  20 23:43 access.log.bak
-rw-r----- 1 nginx adm  2735 5月  20 23:25 error.log

刷新之后的日志:

[root@docker nginx]# ll
总用量 12
-rw-r--r-- 1 nginx root 1900 5月  20 23:46 access.log   #开始写入日志
-rw-r----- 1 nginx adm  3849 5月  20 23:43 access.log.bak  #停止写入
-rw-r----- 1 nginx adm  2735 5月  20 23:25 error.log
[root@docker nginx]# 

猜你喜欢

转载自blog.51cto.com/10950710/2118422
今日推荐