0

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.

2 Answers2

1

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.

Michael Hampton
  • 244,070
  • 43
  • 506
  • 972
  • "The second listen directive binds to both IPv4 and IPv6, unless you explicitly request" -- not true; that only happens on Linux, it will never happen on OpenBSD, for example. – cnst Mar 20 '13 at 21:39
0

You're running Linux?

Might have to do something like this, then:

    listen [::]:80 ipv6only=on;
    listen 80;
cnst
  • 13,848
  • 9
  • 54
  • 76