Fixing “Cannot assign requested address” for NGINX + IPv6 on Ubuntu 18.04

Okay, so before we get started, I’m going to assume the following:

  1. You’re using a host that gives you IPv6 addresses and you do have IPv6 enabled on their end.
  2. You are on Ubuntu 18.04 or later (technically at least 17.10 for this)
  3. 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;
  4. You’re certain the nginx config itself is fine.

Ubuntu 18.04 - Cannot assign requested address in nginx over IPv6

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:

…essentially, 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', 1.2.3.4/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:

  1. Edit the /lib/systemd/system/nginx.service file.
  2. Find the line that says After=network.target and change it to After=network-online.target
  3. Save the file
  4. Run systemctl disable nginx.service followed by systemctl enable nginx.service

The file will look something like this after the modification:

nginx.service file for Ubuntu 18.04

That should be it! Restart your machine and make sure nginx is running!

1 Comment | Leave a Comment

  1. Simon Hampel on November 24, 2018 - click here to reply
    Thanks - this is exactly the problem I just came across having moved from 16.04 to 18.04 and Netplan. Your solution fixed the problem - much appreciated.

Leave a Comment

You can use an alias and fake email. However, if you choose to use a real email, "gravatars" are supported. You can check the privacy policy for more details.