Notifications
Clear all

Configure nginx to auto-restart on failure

1 Posts
1 Users
0 Likes
6 Views
Tharindu
(@tharindu)
Reputable Member Admin
Joined: 10 years ago
Posts: 307
Topic starter  

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.

nginx -t

If the configuration is fine, you can configure systemd to auto-restart nginx on failure. Create a new directory inside systemd.

mkdir /etc/systemd/system/nginx.service.d/

Create override.conf inside the new directory.

nano /etc/systemd/system/nginx.service.d/override.conf

Paste following content,

[Unit]
StartLimitIntervalSec=500
StartLimitBurst=5

[Service]
Restart=on-failure
RestartSec=5s

Reload daemon service,

systemctl daemon-reload

 

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.

This topic was modified 1 year ago by Tharindu

   
Quote
Topic Tags
Share:
Back to top button