在 Nginx 中,重启服务有多种方式,具体取决于你的操作系统和需求。以下是常见的重启命令:
这种方式会重新加载配置并优雅地重启 worker 进程,不会中断现有连接:
bash
sudo systemctl restart nginx
或者:
bash
sudo service nginx restart
如果你只是修改了 Nginx 的配置文件(如 nginx.conf
或站点配置),可以只重新加载配置而不完全重启:
bash
sudo systemctl reload nginx
或者:
bash
sudo service nginx reload
或者直接使用 Nginx 命令:
bash
sudo nginx -s reload
如果 Nginx 出现问题,可能需要先停止再启动:
bash
sudo systemctl stop nginx
sudo systemctl start nginx
或者:
bash
sudo service nginx stop
sudo service nginx start
在重启或重载之前,建议先检查配置文件是否有语法错误:
bash
sudo nginx -t
如果配置正确,会显示:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
如果没有 systemd 或 service 管理工具,可以直接调用 Nginx 二进制文件: ```bash # 重启 sudo nginx -s stop && sudo nginx
# 重新加载配置 sudo nginx -s reload ```
sudo
权限或 root 权限。nginx -t
测试语法,避免重启失败导致服务中断。选择适合你场景的方式即可!