4

Every time I turn on my computer my nginx is not activated:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Qui 2017-07-27 07:11:02 BRT; 59s ago
  Process: 1006 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)

Jul 27 07:11:01 note-gui systemd[1]: Starting A high performance web server and a reverse proxy server...
Jul 27 07:11:02 note-gui nginx[1006]: nginx: [emerg] host not found in upstream "localhost" in /etc/nginx/sites-enabled/default:27
Jul 27 07:11:02 note-gui nginx[1006]: nginx: configuration file /etc/nginx/nginx.conf test failed
Jul 27 07:11:02 note-gui systemd[1]: nginx.service: Control process exited, code=exited status=1
Jul 27 07:11:02 note-gui systemd[1]: Failed to start A high performance web server and a reverse proxy server.
Jul 27 07:11:02 note-gui systemd[1]: nginx.service: Unit entered failed state.
Jul 27 07:11:02 note-gui systemd[1]: nginx.service: Failed with result 'exit-code'.
~

If i do:

sudo systemctl restart nginx

it works fine, but I have to do that every time I start my Ubuntu.

How can I fix this problem so I don't have to restart nginx every time? I use Ubuntu 16.04.

Zanna
  • 70,465
  • Add to /lib/systemd/system/nginx.service the parameter Restart=always – Ziazis Jul 27 '17 at 10:18
  • @Ziazis as far as I know you shouldn't modify the system services but you can copy them to /etc/systemd/system otherwise system updates could wipe out your changes. – Lightbulb1 Sep 04 '19 at 20:52
  • @Ziazis just gave the Restart=always solution a go and it works great. I also added RestartSec=5s just to slow down the restart loop if the network never starts resolving domains. I'm running it on a raspberry pi that I'm using as a local package repository cache so its not the most important thing. – Lightbulb1 Sep 04 '19 at 21:07

1 Answers1

6

The service runs nginx -t (test) before starting the server and this threw up an error:

host not found in upstream "localhost" in /etc/nginx/sites-enabled/default:27

That means you're making a reference to localhost, probably as part of a proxy_pass directive (just guessing) on line 27 of /etc/nginx/sites-enabled/default. And Nginx can't resolve it.

This might be cause for broader concern, but the most simple way around this is to just replace localhost with 127.0.0.1.

Oli
  • 293,335
  • 1
    Still interesting though, haven't used nginx myself in awhile. But that means it is started before the host can lookup himself? So adjusting the start order would be probably also usefull to start it later, since like he said after he manually restarts it, it works without issues. – Ziazis Jul 27 '17 at 10:25
  • perfect awnser, i just change localhost to 0.0.0.0 , restart and works fine, my mistake don't read the msg error – Guilherme Lima Jul 27 '17 at 10:29