ngnix startup isse ipv4 & ipv6

gbuckingham89 asked:

I’m trying to get my first nginx server setup, and am having trouble setting up ipv4 and ipv6.

I have this in the top of my config file (it’s the only server block in the config file):

    server {

            # Listen for requests on these ports 
            listen 80 default;
            listen [::]:80 default_server;
    }

And this is the error I get when I try to start ngnix:

* Restarting nginx nginx
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

Any help is appreciated.

My answer:


You tried to bind to IPv4 twice.

        listen 80 default;
        listen [::]:80 default_server;

By default (on Linux) the second listen directive binds to both IPv4 and IPv6, unless you explicitly request that it bind only to IPv6 with the ipv6only option.

There are two ways you can resolve this. Choose one (but not both):

  1. Delete the first listen directive as redundant. The second directive will cover both IPv4 and IPv6 connections via IPv4-mapped IPv6. The only down side to this is that your logs will show IPv4 addresses in mapped form, e.g. ::ffff:203.0.113.25.

  2. Add ipv6only=on to the second listen directive.


Note that as of nginx 1.3.4, ipv6only=on is now the default, which makes your original configuration necessary in these later versions. Prior to this, the default was operating system dependent.


View the full question and answer on Server Fault.

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