【学习心得 ● 运维】nginx 常用命令(烦人的Nginx)

发布时间:2026/6/24 12:22:46
【学习心得 ● 运维】nginx 常用命令(烦人的Nginx)
nginx 常用命令不算多按使用场景给梳理一遍一、查看信息nginx -v # 只看版本号 nginx -V # 大写V看版本 编译时的配置参数和已编译的模块排查某模块为啥不生效时很有用 nginx -t # 测试配置文件语法是否正确 nginx -T # 大写T测试语法 把最终生效的完整配置全部打印出来含所有 include 进来的文件调试配置神器nginx -V和nginx -T这俩大写的版本知道的人不多但很好用。比如你想确认某个模块像--with-http_ssl_module有没有编进去用nginx -V一看便知想看所有include引入的配置最终拼成什么样nginx -T一次全给你。nginx 是用 systemd 管理时控制服务优先用systemctl而不是nginx -ssystemctl reload nginx # 重载配置等价于 nginx -s reload systemctl restart nginx # 完整重启停掉再启动 systemctl stop nginx # 停止 systemctl start nginx # 启动 systemctl status nginx # 看状态 systemctl enable nginx # 设置开机自启 systemctl disable nginx # 取消开机自启标准做法是这两条连用nginx -t systemctl reload nginx先nginx -t测配置文件的语法表示只有语法测试通过了才执行后面的 reload——这样能避免因为配置写错导致服务挂掉是改 nginx 配置时最稳妥的习惯。二、控制服务nginx -s reload # 重载配置平滑不中断现有连接改完配置用这个 nginx -s reopen # 重新打开日志文件做日志切割/轮转后用让 nginx 重新生成日志文件 nginx -s stop # 快速停止直接掐断不等请求处理完 nginx -s quit # 优雅停止等当前请求处理完再退出生产环境推荐用这个stop和quit的区别值得记一下stop是立即强制停quit是等手头的请求都处理完了再停对线上服务更友好。三、指定配置文件启动/测试nginx -c /path/to/nginx.conf # 用指定的配置文件启动不用默认路径时 nginx -t -c /path/to/nginx.conf # 测试指定的配置文件