Configure nginx to auto-restart on failure
I recently came across a problem with a nginx server where it goes down randomly after some period of time. Whether this is related to Segfault bug or just OOM Killer doing its duty, you can configure nginx to auto-restart after a failure as a temporary workaround. You can avoid hours of downtime and having to manually start nginx using this method.
First you need to make sure there are no configuration errors in your nginx. Following command will help with that.
If the configuration is fine, you can configure systemd to auto-restart nginx on failure. Create a new directory inside systemd.
Create override.conf inside the new directory.
Paste following content,
[Unit] StartLimitIntervalSec=500 StartLimitBurst=5 [Service] Restart=on-failure RestartSec=5s
Reload daemon service,
And then restart nginx,
systemctl restart nginx.service
This will restart nginx 5 seconds after the failure.
You can control how long systemd should wait to restart nginx by adjusting RestartSec value.
This gives you time to find and troubleshoot the actual error without worrying about the downtime.