Okay, so before we get started, I’m going to assume the following:
- You’re using a host that gives you IPv6 addresses and you do have IPv6 enabled on their end.
- You are on Ubuntu 18.04 or later (technically at least 17.10 for this)
- You put an IPv6 address (ie X:X:X:1:2:3:4) as a listen directive in an nginx server block. Example:
listen [XX:XX:XXXX:XX:1:2:3:4]:443 ssl http2;
- You’re certain the nginx config itself is fine.
I hit the “cannot assign requested address” in 2 circumstances. First, nginx wouldn’t start at all because it wouldn’t bind. Once that was fixed, the second issue was it would bind except when the server restarted though it worked when the server was manually restarted.
I’ve run into these in years past, but things changed between Ubuntu 16.04 and 18.04. Beginning in 17.10, Ubuntu changed from ifupdown to netplan which made the process a little different.
In any case, here’s how I fixed each:
NGINX not starting at all when there is an ipv6 listen directive
If you take a look at your
/etc/network/interfaces file, you’ll probably find it empty except for a message mentioning that “ifupdown has been replaced by netplan(5) on this system”.
The new configuration is in
/etc/netplan/10-ens3.yaml. Edit it and you’ll see something like this:
You’ll have to add the IPv6 address here, so it looks like this:
addresses: ['1234:5678:9abc:def0:1:2:3:4/64'] was added. Note the indentation and that you need the prefix (/64). There are prefix calculators on the web if you’re not sure. This was all I needed in my case. However if you have a few to add, they go in the same block but are comma-separated. You should be able to add IPv4 addresses here too, so
addresses: ['x:x:x:x:1:2:3:4/64', 'x:x:x:x:4:3:2:1/64', 220.127.116.11/24] would be an example of that.
Once you’re all set, you need to run the following:
netplan generate netplan apply
…if there was an issue, the first line will usually spit out the problem. If everything went well, try:
service nginx start
Hopefully nginx starts up now!
If you run into other hiccups or if your server was configured a little differently and the above doesn’t quite work, Ubuntu does have a little more on their blog at https://blog.ubuntu.com/2017/12/01/ubuntu-bionic-netplan. Another site with some configuration examples can be found at https://websiteforstudents.com/configuring-static-ips-ubuntu-17-10-servers/.
Issue #2: nginx now works if manually started, but has the “bind / requested address” error when the server is rebooted
You won’t know if you have this issue until you reboot and try a
service nginx status to see the error, followed by a
service nginx start to verify it does work when manually started.
Whether you hit this issue is probably going to depend on the way your host has the network set up. IPv4 tends to come up fast in the networking process, but IPv6 can potentially take awhile. If nginx starts before the IPv6 address is up… well… nginx doesn’t start.
To fix the issue, we want to make nginx wait not just for the “network-online” signal before it starts. This takes place after the normal “network” signal. To do this:
- Edit the
- Find the line that says
After=network.targetand change it to
- Save the file
systemctl disable nginx.servicefollowed by
systemctl enable nginx.service
The file will look something like this after the modification:
That should be it! Restart your machine and make sure nginx is running!