Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

nginx says duplicate default server and will not load

Errornginx: [emerg] a duplicate default server for 0.0.0.0:80

Short answer

A default server belongs to an address and port pair, so only one server block may carry default_server for each pair. The error means two do, or one block is loaded twice by an include. Find every listen line with default_server using nginx -T, and keep exactly one.

  • Applies to nginx 1.26, nginx 1.28
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Dump the whole configuration as nginx sees it and list every default_server, with the file it came from.

    sudo nginx -T 2>&1 | grep -n -E "configuration file|default_server"
  2. 2Match what you see, then jump to its fix

    1. 1nginx -T shows two listen lines with default_server on the same address and port, in different filesTwo server blocks both carry default_server for the same portFix
    2. 2nginx -T prints the same server block under two file names, or a file appears twice in the outputAn include mask loads the same server block twiceFix

What does the error look like?

  • nginx: [emerg] a duplicate default server for 0.0.0.0:80

Cause 1 of 2

Two server blocks both carry default_server for the same port

1Check

The default server is a property of the listen port and not of the server name. Without the parameter, the first server for the pair is the default, so you only need it on one.

sudo nginx -T 2>&1 | grep -n -E "configuration file|listen .*default_server"

2Fix

Keep default_server on the block that should answer unknown hosts, and remove it from the others.

server {
    listen 80 default_server;
    server_name _;
    return 444;
}

server {
    listen 80;
    server_name example.com;
}

3Verify

Test the configuration and reload.

sudo nginx -t && sudo nginx -s reload

Confirmed by nginx.org

Did this fix it?

Cause 2 of 2

An include mask loads the same server block twice

1Check

include accepts a file or a mask, so a wildcard can pick up a copy, a backup or a file that another include already loads.

sudo nginx -T 2>&1 | grep -n "configuration file"
grep -R -n "include" /etc/nginx/nginx.conf

2Fix

Remove the extra copy, or narrow the mask so each file is read once.

http {
    include /etc/nginx/conf.d/*.conf;
}

3Verify

Run the test and check that each server block appears once.

sudo nginx -t
sudo nginx -T 2>&1 | grep -c "server_name example.com"

Confirmed by nginx.org

Did this fix it?

How do I stop it coming back?

  • Decide which file owns the catch-all block for each port and write that in a comment.
  • Keep editor backup files out of directories read by an include mask.
  • Run nginx -t before every reload.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.