常用命令与服务管理
掌握 Nginx 的常用命令是日常运维和调试的基础。无论是启动服务、重载配置,还是排查日志,熟悉这些命令都能大幅提升管理效率。
一、查看 Nginx 版本与编译信息
nginx -v # 显示版本
nginx -V # 显示编译参数、模块信息
`
这些命令可用于确认当前系统安装的 nginx 版本及其支持模块。
二、启动与停止服务
Linux 系统(systemd 管理)
sudo systemctl start nginx # 启动服务
sudo systemctl stop nginx # 停止服务
sudo systemctl restart nginx # 重启服务
sudo systemctl reload nginx # 平滑重载配置
sudo systemctl status nginx # 查看运行状态
macOS(brew 管理)
brew services start nginx
brew services restart nginx
brew services stop nginx
Windows 系统
nginx.exe # 启动
nginx.exe -s stop # 停止
nginx.exe -s quit # 优雅停止
nginx.exe -s reload # 重载配置
三、验证配置文件语法
修改配置文件后,务必先执行语法检测:
nginx -t
若输出:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
表示配置文件正确无误。
四、日志查看与调试命令
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log
实时查看访问日志与错误日志,有助于排查服务异常与请求问题。
五、服务自启配置
在 Linux 系统中,建议将 nginx 设置为系统启动时自动运行:
sudo systemctl enable nginx
六、更多命令参考
更多 nginx 命令和运维说明可参考 nginx官网文档,其中包含详细的参数说明与进阶使用示例。
相关内容
- 备份与恢复 本节介绍 Nginx 配置备份与恢复方法,包括配置文件备份、日志备份、快速恢复和灾难恢复策略,保障服务安全可靠。
- Nginx 教程:目录结构与配置文件详解 本节 Nginx 教程详细介绍 nginx 目录结构与配置文件组成,帮助你掌握 nginx.conf、sites-enabled、logs 等常见路径及配置含义...
- Nginx 与 Docker 集成 本节介绍 Nginx 与 Docker 的集成方法,包括 Nginx 容器部署、配置卷挂载、端口映射和与后端服务协作,实现容器化高可用 Web 服务。
- FastCGI 工作原理 本节讲解 Nginx 与 FastCGI 的工作原理,包括请求转发、PHP-FPM 处理流程及数据交互方式,帮助你理解 nginx.conf 中动态内容处理机制...
- FastCGI 参数详解 本节详细讲解 Nginx 与 FastCGI 配合时的参数配置,包括 fastcgi_param 指令、标准参数与自定义参数,帮助你精确控制 PHP-FPM 请...
- Nginx 作为前端网关 本节介绍 Nginx 作为前端网关的配置方法,包括反向代理、负载均衡、请求路由和安全控制,实现高性能和高可用 Web 服务。