Nginx and NSD3 don't start on boot because they cannot use the assigned IP

Damn Terminal asked:

Server is a Xen VPS running Ubuntu 12.04 and neither nginx nor NSD3 come up after reboot. The apparent reason for that is that they’re not able to bind to their assigned IP addresses right after boot,

from /var/log/boot.log

* Starting configure network device                                     [ OK ]
* Stopping save kernel messages                                         [ OK ]
* Starting MTA                                                          [ OK ] 
nginx: [emerg] bind() to [2a01:1b0:removed:1c9c]:80 failed (99: Cannot assign requested address)
* Starting nsd3...                                                      [ OK ] 
* Starting configure virtual network devices                            [ OK ]
* Stopping configure virtual network devices                            [ OK ]

from /var/log/nsd.log

[1351715473] nsd[956]: error: can't bind udp socket: Cannot assign requested address
[1351715473] nsd[956]: error: server initialization failed, nsd could not be started

Everything works fine after a couple of seconds, and both nginx and NSD3 can be started.

It seems to me that the problem is in the wrong boot order, nginx and NSD3 are started before the network configuration can fully take place. I worked around it by putting

# nginx and nsd boot fix
sleep 4
/etc/init.d/nsd3 start
/etc/init.d/nginx start

in /etc/rc.local but that’s not a proper solution. What is the right way to handle this issue?

Here’s my basic network configuration, from /etc/network/interfaces
auto eth0

iface eth0 inet static
  address 89.removed.121
  gateway 89.removed.1

iface eth0 inet6 static
  up echo 0 > /proc/sys/net/ipv6/conf/all/autoconf
  up echo 0 > /proc/sys/net/ipv6/conf/default/autoconf
  netmask 64
  gateway 2a01:removed:0001
  address 2a01:removed:7c3b
  up ip addr add 2a01:removed:62bd dev eth0 preferred_lft 0
  up ip addr add 2a01:removed:ce6d dev eth0 preferred_lft 0
  up ip addr add 2a01:removed:3e13 dev eth0 preferred_lft 0
  up ip addr add 2a01:removed:1c9c dev eth0 preferred_lft 0

auto lo
iface lo inet loopback

Those awkward up id addr are there because I wanted to add additional IPs but still use the first one for all traffic originating from the server.

My answer:

The easiest fix for this is to not have nginx or nsd bind to a specific IP address, but to listen for any address. For instance in nginx:

listen [::]:80;

The less easy fix is to fix your network interfaces script. The way you have it configured now, your IP addresses are routed to you, but they are not actually bound to the interface in a way that programs can explicitly listen on them. (They only receive traffic for them if they bound to any address, as above.) To do that, you need additional iface sections, instead of the ip route commands.

iface eth0 inet6 static
        address 2001:db8::1234
        netmask 64

iface eth0 inet6 static
        address 2001:db8::5678
        netmask 64

iface eth0 inet6 static
        address 2001:db8::abcd
        netmask 64

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.