nginx.pid文件丢失,日志无法正常轮转。解决方法:
故障原因,日志被迁移后,kill-USR1 pid 没有成功,致使nginx写的文件句柄还是在旧的文件里。
模拟故障:
1: 我们 mv 日志文件为.bak
2: 我们清空nginx.pid文件
3: 我们试图reload的时候失败,因为pid文件是空的。这时候我们使用killall nginx ,然后再启动nginx才能解决。
[[email protected] nginx]# ps -ef |grep nginx
root 10287 1 0 10:18 ? 00:00:00 nginx: master process/usr/sbin/nginx -c /etc/nginx/nginx.conf
nobody 10290 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10291 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10292 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10295 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10296 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10297 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10298 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10299 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10300 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10301 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10304 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10306 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10307 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10308 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10310 10287 0 10:18 ? 00:00:00 nginx:worker process
nobody 10311 10287 0 10:18 ? 00:00:00 nginx:worker process
root 10713 10080 0 10:24 pts/0 00:00:00 grep --color=auto nginx
[[email protected] nginx]# less/var/run/nginx.pid
10287
[[email protected] nginx]# pwd
/etc/nginx
[[email protected] nginx]# mv/data/logs/access.log /data/logs/access.log.bak
[[email protected] nginx]# cd/data/logs/
[[email protected] logs]# ls
access.log.bak error.log nginx_access.log nginx_error.log nginxlog.sh
[[email protected] logs]# echo "" > /var/run/nginx.pid
[[email protected] logs]#/etc/init.d/nginx reload
Reloading nginx: [FAILED]
平滑解决方法:
1:平滑生成日志,不需要重启。我们发现access.log已经存在。
2:但pid文件还是空的,所以nginx reload是失败的。
3:我们将pid号覆盖掉/var/run/nginx.pid
4:nginx reload 成功,此过程不涉及到任何重启nginx情况,实现0请求丢失。
[[email protected] logs]# ps -ef |grep nginx | grep root | grep -v grep | awk ‘{print $2}‘|xargs -i kill -USR1{}
[[email protected] logs]# ls
access.log access.log.bak error.log nginx_access.log nginx_error.log nginxlog.sh
[[email protected] logs]#/etc/init.d/nginx reload
Reloading nginx: [FAILED]
[[email protected] logs]# ps -ef |grep nginx | grep root | grep -v grep | awk ‘{print $2}‘ |xargs -i echo {} >/var/run/nginx.pid
[[email protected] logs]#/etc/init.d/nginx reload
Reloading nginx: [ OK ]
解决方法对比:
第一种方法,即killallnginx方法解决,然后启动。
1:如果只有单台nginx,会有请求丢失的情况。
2:因为是硬重启,无法启动的可能性。
第二种方法,平滑重启不会丢失任何清理,且不存在任何危险,可以放入脚本中执行。