清华OA nginx重启原因配置

问题找到了

你现在的 nginx.service 里面 ExecStop 还是旧的 /bin/kill -QUIT $MAINPID,这就是根源! 当 systemd 执行停止时,这个命令执行异常,systemd 标记服务失败,但 nginx 进程没杀掉,造成状态不同步。

1. 修改 service 文件,替换 ExecStop

vi /etc/systemd/system/nginx.service

把里面这一行:

ExecStop=/bin/kill -QUIT $MAINPID

改成:

ExecStop=/usr/local/nginx/sbin/nginx -s stop

修改后完整文件:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

保存退出(vi:ESC → :wq回车)

2. 重载systemd配置(必须执行!)

systemctl daemon-reload

3. 杀掉残留的旧nginx进程(PID 5326)

kill -9 5326

检查进程是否清理干净:

ps -ef | grep nginx

只保留grep那一行才算成功。

4. 启动nginx,查看状态

systemctl start nginx
systemctl status nginx

执行完把status的输出贴出来。


原理回顾

  • ExecStop=/bin/kill -QUIT $MAINPID:systemd尝试kill,但脚本执行异常退出,systemd判定服务failed;
  • 但nginx进程本身没死,继续占用80/8080端口;
  • 换成 nginx -s stop,使用nginx原生的停止命令,会读取pid文件优雅关闭,不会出现这种状态错乱。

后续操作nginx统一使用: systemctl start/stop/restart/reload nginx 不要再直接执行 /usr/local/nginx/sbin/nginx 手动启动。





问题找到了

你现在的 nginx.service 里面 ExecStop 还是旧的 /bin/kill -QUIT $MAINPID,这就是根源! 当 systemd 执行停止时,这个命令执行异常,systemd 标记服务失败,但 nginx 进程没杀掉,造成状态不同步。

1. 修改 service 文件,替换 ExecStop

vi /etc/systemd/system/nginx.service

把里面这一行:

ExecStop=/bin/kill -QUIT $MAINPID

改成:

ExecStop=/usr/local/nginx/sbin/nginx -s stop

修改后完整文件:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target

保存退出(vi:ESC → :wq回车)

2. 重载systemd配置(必须执行!)

systemctl daemon-reload

3. 杀掉残留的旧nginx进程(PID 5326)

kill -9 5326

检查进程是否清理干净:

ps -ef | grep nginx

只保留grep那一行才算成功。

4. 启动nginx,查看状态

systemctl start nginx
systemctl status nginx

执行完把status的输出贴出来。


原理回顾

  • ExecStop=/bin/kill -QUIT $MAINPID:systemd尝试kill,但脚本执行异常退出,systemd判定服务failed;
  • 但nginx进程本身没死,继续占用80/8080端口;
  • 换成 nginx -s stop,使用nginx原生的停止命令,会读取pid文件优雅关闭,不会出现这种状态错乱。

后续操作nginx统一使用: systemctl start/stop/restart/reload nginx 不要再直接执行 /usr/local/nginx/sbin/nginx 手动启动。

posted @ 2026-10-13 15:33  郭慕荣  阅读(3)  评论(0)    收藏  举报