5

nginx will not start on boot, but starts fine manually. The logs say something like

nginx: [emerg] host not found in upstream "{some hostname}" in /etc/nginx/conf.d/some_site.conf
nginx: configuration file /etc/nginx/nginx.conf test failed

Failed to start A high performance web server and a reverse proxy server.
nginx.service: Unit entered failed state.
nginx.service: Failed with result 'exit-code'.

It seems that on bootup dns is not ready yet and nginx is trying to lookup a hostname that I have in as a reverse proxy.

1 Answer 1

9

After some research it turns out systemd uses the file below to startup nginx

/lib/systemd/system/nginx.service

The file on Ubuntu 15.04, Ubuntu 15.10, and even the nginx site itself say to use the After line like below

After=network.target

This makes a person think that the network should up and ready to go. However, I found the link http://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/ that says

network.target has very little meaning during start-up. It only indicates that the network management stack is up after it has been reached.

The link goes on to say

network-online.target is a target that actively waits until the nework is "up", where the definition of "up" is defined by the network management software.

So I changed the After use that instead like

After=network-online.target

Now nginx starts up fine on bootup. I am not claiming this is the fully correct solution as it is possible that a different After target should be used that would be more correct in some other way, but this seems to work for the last couple of reboots.

3
  • I keep getting the host not found in upstream error even with After=network-online.target nss-lookup.target.
    – KillianDS
    Feb 12, 2016 at 10:00
  • I have the same problem on RHEL (nginx starts so quickly that upstream connectivity is not yet established). I am a bit hesitant with this solution however. The documentation which you linked also states that "It is strongly recommended not to pull in this target too liberally: for example network server software should generally not pull this in (since server software generally is happy to accept local connections even before any routable network interface is up)". I would much rather fix this in nginx somehow.
    – Vilsepi
    Jun 29, 2016 at 11:59
  • 1
    Yea, this solution worked on a "slow" computer. I upgraded my computer and now it fails again. So I don't know how to fix this.
    – Paul
    Jul 5, 2016 at 14:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .