How to Fix Nginx “Duplicate Listen Options for [::]:80” Error – IPv6 Configuration Guide


1 views

When working with Nginx configurations involving IPv6, you might encounter the error message "duplicate listen options for [::]:80". This typically occurs when there's a conflict in how IPv6 addresses are specified across multiple server blocks or within the same configuration.

The error stems from how Nginx processes the listen directive for IPv6 addresses. The square brackets [::] represent the IPv6 wildcard address (equivalent to 0.0.0.0 in IPv4). When this appears multiple times with conflicting options, Nginx raises the duplicate options error.

# Scenario 1: Multiple default server declarations
server {
    listen [::]:80 default_server;
    ...
}

server {
    listen [::]:80 default_server;
    ...
}

# Scenario 2: Conflicting IPv6 parameters
server {
    listen [::]:80 ipv6only=on;
    listen [::]:80 ipv6only=off;
    ...
}

For a clean IPv6 setup, follow these guidelines:

# Recommended configuration for IPv6
server {
    listen 80;                      # IPv4
    listen [::]:80 ipv6only=on;     # IPv6
    
    # Optional: specify default server on only one block
    # listen 80 default_server;
    # listen [::]:80 default_server ipv6only=on;
    
    server_name example.com;
    ...
}

Always test your Nginx configuration before applying changes:

sudo nginx -t

If the error persists, check for:

  • Included configuration files that might have duplicate directives
  • Virtual host files in /etc/nginx/conf.d/ or /etc/nginx/sites-enabled/
  • Default configurations that might be included automatically

Here's a fully functional configuration that avoids the duplicate listen error:

server {
    listen 80;
    listen [::]:80 ipv6only=on;
    
    root /var/www/html;
    index index.html index.htm;
    
    server_name mydomain.com;
    
    location / {
        try_files $uri $uri/ =404;
    }
    
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    }
    
    location ~ /\.ht {
        deny all;
    }
}
  • Always specify ipv6only=on for IPv6 listen directives
  • Only declare one default server per IP version
  • Use nginx -t to test configurations before reloading
  • Check for configuration conflicts in included files

When working with Nginx IPv6 configurations, particularly with dual-stack setups (IPv4/IPv6), it's common to encounter the "duplicate listen options" error. The error typically occurs when there's conflicting or redundant configuration for the same port.

The original configuration had two problematic aspects:

listen 80 default_server;
listen [::]:80 default_server ipv6only=on;

While removing the default_server parameter helped with the first error, it revealed a deeper configuration issue with IPv6 handling.

For modern Nginx versions (1.3.4+), the ipv6only=on parameter is deprecated and unnecessary. Here's the correct configuration:

server {
    listen 80;
    listen [::]:80;
    
    # Rest of your configuration remains unchanged
    server_name munki;
    root /usr/share/nginx/html;
    # ... other directives ...
}

In rare cases where you need separate IPv4 and IPv6 handling, consider this approach:

# IPv4-only server
server {
    listen 80;
    server_name munki;
    # IPv4 specific configuration
}

# IPv6-only server
server {
    listen [::]:80;
    server_name munki;
    # IPv6 specific configuration
}
  • Don't mix default_server declarations across multiple server blocks
  • Avoid deprecated parameters like ipv6only in modern Nginx versions
  • Ensure all listen directives for the same port have consistent parameters

After making changes, always test your configuration:

sudo nginx -t
sudo systemctl reload nginx

For servers with multiple IP addresses, be explicit in your binding:

server {
    listen 192.168.1.100:80;
    listen [2001:db8::1]:80;
    # Configuration
}