Nginx Error: [emerg] bind() to [::]:80 failed (98: Address already in use)

Problem description

When starting the nginx service, the following error occurred:

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
...

Cause of the problem

There are many reasons for this problem. We will list our scenarios and solutions here.

First, the port is occupied

This is the most common reason. Port 80 is occupied, which leads to binding of nginx service.

Solution 1

Find the process occupying port 80 and end it.

Second, dual stack sockets for IPv6

This is a common problem in the transition period from IPv4 to IPv6. The following configuration will also cause the above error:

server {
    listen [::]:80 ipv6only=off;        
    server_name dual-stack.example.com;
}
server {
    listen 0.0.0.0:80;
    server_name ipv4.example.com;
}

With the IPv6 only = off option, the currently created socket is dual stack, and IPv4 will be mapped to IPv6. At this time, only one monitor can be created, and IPv4 can no longer be monitored.

Solution 2

Since we can’t monitor IPv4, and now it’s a dual stack, we can monitor IPv6 address safely (and ensure IPv4 access at the same time)

server {
    listen [::]:80 ipv6only=off;        
    server_name dual-stack.example.com;
}
server {
    listen [::]:80;
    server_name ipv4.example.com;
}
# Because of the specific scenario, we cannot modify the configuration of the first Server
# Of course, it is also possible to turn off the double stack.

Read More: