How To Fix Nginx "Duplicate Default Server" Error?

Published July 26, 2024

Problem: Duplicate Default Server in Nginx

The "Duplicate Default Server" error in Nginx happens when multiple server blocks are set as the default server for the same IP address and port combination. This setup issue can cause Nginx to respond inconsistently to incoming requests.

Solution: Removing Duplicate Default Server Configurations

Checking the /etc/nginx/sites-enabled Directory

To fix the duplicate default server issue, check the /etc/nginx/sites-enabled directory. This directory has symbolic links to active server block configurations. Look for files with conflicting default_server directives.

To find conflicting configuration files, use this command:

grep -R "default_server" /etc/nginx/sites-enabled

This command shows all "default_server" occurrences in the configuration files. Review the output to find duplicate default_server directives.

After finding the conflicting files, remove or change the unnecessary default server directives. If you have a default configuration file you don't need, remove it with:

sudo rm /etc/nginx/sites-enabled/default

Tip: Backup Before Removal

Before removing any configuration files, create a backup to avoid losing important settings. Use this command to create a backup:

sudo cp /etc/nginx/sites-enabled/default /etc/nginx/sites-enabled/default.bak

Modifying the Nginx Configuration

If you need to keep multiple server blocks, change the listen directive in your configuration files. Make sure only one server block has the default_server parameter for each IP address and port combination.

To change a configuration file, use a text editor like nano:

sudo nano /etc/nginx/sites-enabled/your_config_file

Change the listen directive to remove or add the default_server parameter as needed. For example:

listen 80;  # Remove default_server if it's not needed

Or, if you want this server block to be the default:

listen 80 default_server;

Remember, use the default_server parameter only once per IP:port combination across all your configuration files.

After making these changes, save the file and exit the text editor. Repeat this process for other configuration files that need changes.

Verifying the Fix

Restarting Nginx

After changing your Nginx configuration, test and restart the service to apply the changes. Use this command to check if your configuration is valid:

sudo nginx -t

This command tests the Nginx configuration files for syntax errors. If the test succeeds, you'll see a message confirming the configuration test is successful.

If the test passes, restart the Nginx service to apply the changes:

sudo systemctl restart nginx

For systems not using systemd, use:

sudo service nginx restart

Tip: Graceful Restart

To restart Nginx without interrupting active connections, use the following command:

sudo systemctl reload nginx

This method reloads the configuration without dropping connections.

Monitoring Error Logs

After restarting Nginx, check the error logs to confirm the duplicate default server error is gone. Access the Nginx error log with this command:

sudo tail -f /var/log/nginx/error.log

This command shows the latest entries in the error log in real-time. Look for any new occurrences of the duplicate default server error.

To check if your server works correctly, try accessing your website through a web browser. If the site loads without problems, it's a good sign that the configuration works properly.

You can also use this command to check if Nginx is running without errors:

sudo systemctl status nginx

This command shows the current status of the Nginx service, including recent error messages or warnings.

If you don't see any errors related to duplicate default servers and your website is accessible, you have fixed the issue.