nginx 启动与停止
Nginx 服务的启动、停止、重载配置和状态检查
语法
nginx [-s signal] [-c config] [-t]
参数
| 参数 | 说明 | 示例 | 级别 |
|---|---|---|---|
(无参数) |
启动 Nginx | nginx |
常用 |
-s stop |
快速停止 | nginx -s stop |
常用 |
-s quit |
优雅停止(等待请求处理完) | nginx -s quit |
常用 |
-s reload |
重载配置(不中断服务) | nginx -s reload |
常用 |
-t |
测试配置文件语法 | nginx -t |
常用 |
-c file |
指定配置文件 | nginx -c /etc/nginx/nginx.conf |
进阶 |
示例
启动 Nginx
sudo systemctl start nginx # 或直接 sudo nginx
修改配置后重载
sudo nginx -t && sudo nginx -s reload
先测试语法再重载,避免配置错误导致服务中断
查看运行状态
sudo systemctl status nginx
查看 Nginx 版本和编译参数
nginx -V
大写 V 显示详细编译信息
常见错误
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
80 端口被占用,用 lsof -i :80 查找并停止占用进程
nginx: [emerg] "server" directive is not allowed here
配置文件语法错误,server 块应该在 http 块内
技巧
- 修改配置后一定先 nginx -t 测试再 reload
- 用 systemctl enable nginx 设置开机自启
- nginx -s reload 是零停机重载,生产环境推荐