shell脚本代码集

Nginx命令最全收集

Nginx是大型架构的核心,下面是收集的常用的Nginx命令:
1、 启动 Nginx

nginx

或:

systemctl start nginx

作用:启动 Nginx 主进程(master + worker)

2、停止 Nginx立即停止)

nginx -s stop

特点:直接杀 worker 进程(可能丢请求)

3、优雅停止Nginx(推荐)

nginx -s quit

特点:等当前请求处理完再退出更安全(生产环境推荐)

4、重启 Nginx

systemctl restart nginx

等价于:

stop + start
  1. 平滑重载配置(最常用)
    nginx -s reload
    
    核心机制:不停止服务worker 进程平滑切换零宕机更新配置, 生产环境最重要命令之一

6、重新打开日志文件

nginx -s reopen

用途:日志切割(logrotate)避免日志丢失

7、检查配置语法

nginx -t

输出示例:

nginx: configuration file /etc/nginx/nginx.conf test is successful

8、指定配置文件检查

nginx -t -c /etc/nginx/nginx.conf

用途:多环境配置验证

9、 查看 Nginx 编译参数

nginx -V

包含:编译模块路径信息版本信息

10、查看 Nginx 进程

ps -ef | grep nginx

11、查看

master PID
cat /run/nginx.pid

或:

cat /var/run/nginx.pid

12、手动杀进程(慎用)

kill-9<pid>

不推荐原因:可能导致连接中断不优雅关闭五、日志查看类(排错核心)
13、实时访问日志

tail -f/var/log/nginx/access.log

14、实时错误日志

tail -f/var/log/nginx/error.log

15、过滤错误日志

grep "error"/var/log/nginx/error.log