Ubuntu24.04设置Nginx自动重启

Title Ubuntu24.04设置Nginx自动重启
Framework Ubuntu
User wy8817399@vip.qq.com
Id 44
Created 2/9/26, 7:50 AM
Modified 2/9/26, 9:54 AM
Published Yes
Content

1、创建 override 目录

sudo mkdir -p /etc/systemd/system/nginx.service.d

 

2、创建自动重启配置

sudo tee /etc/systemd/system/nginx.service.d/restart.conf <<'EOF'

[Service]
# 进程异常退出时自动重启
Restart=on-failure

# 正常退出也重启(可选,更严格)
# Restart=always

# 退出后延迟 5 秒重启
RestartSec=5s

# 重启限制(防止频繁崩溃)
StartLimitInterval=60s
StartLimitBurst=5

# 优雅停止超时时间
TimeoutStopSec=10s

EOF

 

3、重载 systemd 配置

sudo systemctl daemon-reload

 

4、重启 nginx 使配置生效

sudo systemctl restart nginx

 

5、验证配置生效

systemctl show nginx | grep -E "Restart|StartLimit|TimeoutStopSec"