Skip to content

Nginx 服务管理(restart / reload / start / stop)

  • 状态:已验证
  • 来源:对话整理
  • 更新时间:2026-03-16

关键结论

  • 使用 systemd 的系统推荐通过 systemctl 管理 nginx 服务。
  • reload 用于重新加载配置,不会主动中断现有连接。
  • restart 会停止并重新启动 nginx,现有连接会中断。
  • 修改配置后应先执行 nginx -t 检查配置语法。
  • 生产环境优先使用 reload,只有确实需要完整重启时才使用 restart

详细分析

  • reload 的本质是向 nginx master 进程发送重新加载配置的信号,使其读取新配置并拉起新的 worker 进程。
  • 旧 worker 一般会在处理完当前请求后再退出,因此 reload 通常适合在线变更配置。
  • restart 等价于先停止再启动服务,期间会有短暂不可用窗口,现有连接和请求会受到影响。
  • 如果配置文件存在语法错误,reload 通常会保留当前正在运行的旧配置,不会直接把服务打挂。
  • 相比之下,如果直接执行 restart 且新配置有误,服务可能会重启失败,导致 nginx 无法恢复上线。
  • 因此典型流程应是“先检查配置,再选择 reload 或 restart”,而不是直接重启。

可执行步骤

  1. 修改 nginx 配置文件。

  2. 检查配置是否正确:

bash
nginx -t
  1. 如果只是变更配置并希望平滑生效,重新加载服务:
bash
sudo systemctl reload nginx
  1. 如果需要完整重启服务,再执行:
bash
sudo systemctl restart nginx

命令 / 配置 / 代码

bash
# 检查配置
nginx -t

# 重新加载配置(推荐)
sudo systemctl reload nginx

# 重启 nginx
sudo systemctl restart nginx

# 启动 nginx
sudo systemctl start nginx

# 停止 nginx
sudo systemctl stop nginx

# 查看 nginx 状态
systemctl status nginx

# 如果修改了 nginx.service 等 systemd 单元文件
systemctl daemon-reload

风险与注意事项

  • restart 会断开现有连接,生产环境应谨慎使用。
  • 在执行 reloadrestart 之前,建议先运行 nginx -t 做配置检查。
  • 如果修改的是 systemdnginx.service 单元文件,而不是 Nginx 自身配置文件,需要先执行 systemctl daemon-reload
  • reload 更适合 Nginx 配置变更;daemon-reload 只影响 systemd 对 unit 文件的重新读取,两者不能混淆。