Comment 1 for bug 1818574

Andreas Hasenack (ahasenack) wrote :

Thanks for filing this bug in Ubuntu.

There are similar cases out there with other services, when a specific IP address is selected for the service to bind to. openssh, for example, has this bug upstream: https://bugzilla.mindrot.org/show_bug.cgi?id=2512

A few solutions, or workarounds, exist for this. The only one that seems to work for all use cases is to use the IP_FREEBIND socket option:
       IP_FREEBIND (since Linux 2.4)
              If enabled, this boolean option allows binding to an IP address that is nonlocal or does not (yet) exist. This permits listening on a socket, without requiring the underlying network interÔÇÉ
              face or the specified dynamic IP address to be up at the time that the application is trying to bind to it. This option is the per-socket equivalent of the ip_nonlocal_bind /proc interface
              described below.

That requires an upstream change, however.

In the meantime, users affected by this are probably better off by having nginx starting later in the boot process by adding a dependency to network-online.target. It will likely make the boot slower, though, that's why it's not a good solution to be applied generally.

You can try this perhaps:
sudo systemctl edit nginx.service

Then in the empty file that opens, type:
[Unit]
After=network-online.target
Requires=network-online.target

Then save. You can view your addition in the output of this command now:

sudo systemctl cat nginx.service

There appear to be more fine-grained ways of achieving this, like specifying which interface to wait for. https://www.freedesktop.org/software/systemd/man/systemd-networkd-wait-online.html documents something like that, but I haven't tried it.