教程 nginx tutorial

常用命令与服务管理

掌握 Nginx 的常用命令是日常运维和调试的基础。无论是启动服务、重载配置,还是排查日志,熟悉这些命令都能大幅提升管理效率。

一、查看 Nginx 版本与编译信息

bash
nginx -v        # 显示版本
nginx -V        # 显示编译参数、模块信息

`

这些命令可用于确认当前系统安装的 nginx 版本及其支持模块。

二、启动与停止服务

Linux 系统(systemd 管理)

bash
sudo systemctl start nginx      # 启动服务
sudo systemctl stop nginx       # 停止服务
sudo systemctl restart nginx    # 重启服务
sudo systemctl reload nginx     # 平滑重载配置
sudo systemctl status nginx     # 查看运行状态

macOS(brew 管理)

bash
brew services start nginx
brew services restart nginx
brew services stop nginx

Windows 系统

bash
nginx.exe           # 启动
nginx.exe -s stop   # 停止
nginx.exe -s quit   # 优雅停止
nginx.exe -s reload # 重载配置

三、验证配置文件语法

修改配置文件后,务必先执行语法检测:

bash
nginx -t

若输出:

txt
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

表示配置文件正确无误。

四、日志查看与调试命令

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

实时查看访问日志与错误日志,有助于排查服务异常与请求问题。

五、服务自启配置

在 Linux 系统中,建议将 nginx 设置为系统启动时自动运行:

bash
sudo systemctl enable nginx

六、更多命令参考

更多 nginx 命令和运维说明可参考 nginx官网文档,其中包含详细的参数说明与进阶使用示例。