Nginx 服务管理常用命令
一、systemd 标准管理
# 启动 Nginx
systemctl start nginx
# 停止 Nginx(优雅关闭,等待连接处理完)
systemctl stop nginx
# 重启 Nginx(先停再启,会中断现有连接)
systemctl restart nginx
# 重载配置(热更新,不中断连接,生产优先使用)
systemctl reload nginx
# 设置开机自启
systemctl enable nginx
# 取消开机自启
systemctl disable nginx
# 查看运行状态
systemctl status nginx
# 查看完整日志
journalctl -u nginx
# 实时跟踪日志
journalctl -u nginx -f
二、配置文件校验
修改配置必须先执行
# 基础语法检查 nginx -t
# 同时打印加载的配置文件路径
nginx -T
> 只有 `-t` 显示 `test is successful` 再执行 reload!
## 三、二进制原生控制(源码编译、无systemd环境)
> 前提:知道 nginx 可执行文件路径,一般 `/usr/local/nginx/sbin/nginx`
```bash
# 启动
/usr/local/nginx/sbin/nginx
# 优雅停止(等待请求处理完毕)
/usr/local/nginx/sbin/nginx -s quit
# 强制立即停止(直接断开连接)
/usr/local/nginx/sbin/nginx -s stop
# 重载配置
/usr/local/nginx/sbin/nginx -s reload
# 重新打开日志文件(日志切割后使用)
/usr/local/nginx/sbin/nginx -s reopen
# 指定配置文件启动
/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
四、常用查看信息命令
# 查看 Nginx 版本
nginx -v
# 查看版本 + 编译参数(非常重要,排查模块缺失)
nginx -V
# 查看帮助
nginx -h
五、进程相关操作
# 查看nginx进程
ps -ef | grep nginx
# 或者
pgrep nginx
# 查看监听端口
ss -tlnp | grep nginx
netstat -tlnp | grep nginx
六、日志切割配套命令
日志轮转工具切割 access.log / error.log 之后,需要通知nginx重新打开日志:
nginx -s reopen
# 或二进制路径
/usr/local/nginx/sbin/nginx -s reopen
七、常见故障快速命令
# 重载失败时,单独测试配置
nginx -t
# 查看最近 nginx 崩溃日志
journalctl -u nginx -n 100
八、简易使用规范
- 修改配置 → nginx -t
- 验证通过 → systemctl reload nginx
- 尽量避免频繁
restart,优先reload - 日志切割脚本末尾务必加上
-s reopen