6

I am using crontab to start some services after a reboot.

Here is my current crontab:

@reboot root /etc/init.d/nginx reload
@reboot /usr/local/bin/forever start /var/www/rtc/index.js

It works for forever, but nginx never starts.

I also tried:

@reboot /etc/init.d/nginx reload
@reboot sudo service nginx reload

Any ideas?

My nginx conf looks like this:

server {
        listen 443 ssl;
        server_name www.example.com;

        ssl_certificate /var/wwwssl/example.pem;
        ssl_certificate_key /var/wwwssl/example.key.pem;

        location / {
                proxy_pass https://www.example.com:8443;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
}

EDIT:

This solution also did not work

Do that with a systemd drop-in:

[Service]
Restart=always

which you place as the file /etc/systemd/system/nginx.service.d/override.conf (creating the directory if it doesn't exist). You can also use systemctl edit nginx to create the file.

EDIT:

The service is enabled.

# systemctl is-enabled nginx
enabled

I still have no clue why nginx does not start.

After every shutdown -r (I use this to test reboot) I check with sudo service --status-all and nginx is not running.

EDIT

The syslogs show some errors for nginx after reboot:

nginx: [emerg] host not found in upstream "www.example.com" in /etc/nginx/sites-enabled/default:100
nginx: configuration file /etc/nginx/nginx.conf test failed
nginx.service: Control process exited, code=exited, status=1/FAILURE
nginx.service: Failed with result 'exit-code'.
Failed to start A high performance web server and a reverse proxy server.

EDIT:

Tried to add a resolver:

resolver IP valid=30s;

Still the same issue

EDIT

It may be that nginx cant start after reboot, because of the start order? Nginx must be started by root. The node app is started by nodeuser with crontab.

Manually it works:

  1. I reboot the server
  2. After reboot the node app is running (crontab starts the forever process)
  3. Nginx has errors
  4. I start nginx with service nginx restart

I assume what leads to the problem is:

After reboot the root process is handled first. Nginx tries to start, but the node app is not started yet, so it bugs out. But how would I fix that?

I seems order makes no difference. I removed the forever start and after reboot the same errors show up.

nginx -t:

enter image description here

12
  • 2
    Why you need to start these service by cron? Doesn't sudo systemctl enable service.name work? – pa4080 Jun 15 '20 at 12:12
  • @pa4080 Sadly it also did not work. – Roman Jun 15 '20 at 12:37
  • The problem is probably related to the error, try to google it. You can also insert here picture of you Nginx configuration file. – John Ronald Jun 16 '20 at 14:29
  • 1
    Well, does the server www.example.com exist ? (In your LAN) – John Ronald Jun 16 '20 at 15:00
  • 1
    Do not use crontab for this, it too complicated for this. Use systemctl enable nginx – John Ronald Jun 23 '20 at 8:00
2

Roman:

sudo systemctl enable nginx

Can you try with this command to set link to init as SysV?

update-rc.d nginx defaults

Why set nginx defaults?

When  run  with  the  defaults  option,  update-rc.d  makes links named
       /etc/rcrunlevel.d/[SK]NNname that point to the script /etc/init.d/name,
       using  runlevel  and  dependency information from the init.d script LSB
       comment header.
1
  • This also did not help. – Roman Jun 16 '20 at 12:31
1

The solution is also explained in this stack topic

I had to replace the domain in proxy_pass in the nginx config with the IP:

proxy_pass https://<IP-OF-YOUR-SERVER>:8443;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.