nginx says duplicate default server and will not load
nginx: [emerg] a duplicate default server for 0.0.0.0:80Short 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
nginx -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
nginx -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
- 1
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 reloadConfirmed by nginx.org
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.conf2Fix
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
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
Servers
Angular routes return 404 from nginx when you refresh or open a link
The home page loads, but refreshing /some/route shows an nginx 404
Angular routes like /users/42 are not files, so nginx looks for them on disk and returns 404.
3 causes, each with a fixServers
Argo CD behind an nginx Ingress loops with too many redirects
307 Temporary Redirect on every request to the Argo CD host
The browser and Argo CD disagree about who handles TLS.
3 causes, each with a fixServers
A Let's Encrypt certificate did not renew with Certbot
Type: connection ... Timeout during connect (likely firewall problem)
Run sudo certbot renew --dry-run and read the Type and Detail lines.
6 causes, each with a fixServers
curl cannot connect to port 80 or 443 on a server running nginx
CURLE_COULDNT_CONNECT (7): Failed to connect() to host or proxy
Exit code 7 means the TCP connection itself failed, before any HTTP happened.
3 causes, each with a fixServers
Django returns a 500 behind Gunicorn and the traceback is nowhere
The traceback shows locally with runserver but not under Gunicorn
With DEBUG set to False, Django's default logging does not print errors to the console.
2 causes, each with a fixServers
Docker php image has no php.ini and my edits do not stay
A setting changed in php.ini inside the container is gone after the container is recreated
The official php image ships php.ini-development and php.ini-production but no active php.ini.
2 causes, each with a fix