Nginx 常用管理命令、启动/停止方式、信号控制方式,以及如何使用 Crontab 设置定时重启任务。适用于 Ubuntu / Debian / CentOS 等系统。
一、查看 Nginx 基本信息
1. 查看 Nginx 版本
nginx -v
2. 查看详细版本和编译参数
nginx -V
二、检查 Nginx 配置
1. 检查配置是否正确
nginx -t
2. 显示完整配置内容(含 include)
nginx -T
3. 检查配置时禁止输出非错误信息
nginx -q
三、查看 Nginx 进程
1. 查看运行中的进程
ps -aux | grep nginx
常见进程结构:
- master process(主进程)
- worker process(工作进程)
四、Nginx 主进程信号控制
使用 nginx -s 的方式向主进程发送指令。
nginx -s stop # 立即停止(强制)
nginx -s quit # 优雅退出(等待请求处理完)
nginx -s reopen # 重新打开日志文件
nginx -s reload # 重载配置(推荐)
推荐使用 reload 重载配置,不会中断请求服务。
五、Nginx 服务管理(SysV / Systemd)
不同发行版可能使用 service 或 systemctl。
1. 启动 Nginx
service nginx start
或
systemctl start nginx
2. 停止 Nginx
service nginx stop
或
systemctl stop nginx
3. 重启 Nginx
service nginx restart
或
systemctl restart nginx
4. 重载配置
service nginx reload
或
systemctl reload nginx
5. 设置开机自启动
systemctl enable nginx
可取消:
systemctl disable nginx
六、使用 Crontab 定时重启 Nginx
定时重启可防止长时间运行导致内存泄漏或僵尸进程等问题。
1. 管理 cron 服务
/etc/init.d/cron status # 查看状态
/etc/init.d/cron start # 启动 cron(旧系统)
/etc/init.d/cron stop # 停止 cron
/etc/init.d/cron reload # 重载 cron 配置
service cron start # 启动 cron
service cron restart # 重启 cron 服务
2. Crontab 常用命令
crontab -e # 编辑当前用户定时任务
crontab -l # 查看当前用户任务
七、常用 Nginx 定时重启任务示例
1)每 3 分钟重启一次
*/3 * * * * systemctl restart nginx
2)每 3 小时重启一次
0 */3 * * * systemctl restart nginx
3)每天凌晨重启一次
0 0 * * * systemctl restart nginx
4)每周日凌晨重启一次
0 0 * * 0 systemctl restart nginx
5)每月 1 日凌晨重启一次
0 0 1 * * systemctl restart nginx
6)每年 1 月 1 日凌晨重启一次
0 0 1 1 * systemctl restart nginx








