How to Fix Conflicting Server Name Error in Nginx
The conflicting server name error in Nginx happens when two or more server blocks use the same
server_name. To fix it, ensure each server_name is unique or combine configurations if they should share the same name.Why This Happens
This error occurs because Nginx requires each server_name to be unique across all server blocks. When two server blocks declare the same server_name, Nginx cannot decide which block to use for incoming requests, causing a conflict.
nginx
server {
listen 80;
server_name example.com;
root /var/www/site1;
}
server {
listen 80;
server_name example.com;
root /var/www/site2;
}Output
nginx: [emerg] conflicting server name "example.com" on 0.0.0.0:80, ignored
The Fix
To fix the conflict, make sure each server_name is unique. If you want to serve the same domain with different settings, combine them into one server block or use different ports or IP addresses.
nginx
server {
listen 80;
server_name example.com;
root /var/www/site1;
# Add other settings here if needed
}
server {
listen 80;
server_name example.org;
root /var/www/site2;
}Output
nginx: configuration file /etc/nginx/nginx.conf test is successful
Prevention
To avoid this error in the future, always check your Nginx configuration files for duplicate server_name entries before restarting. Use tools like nginx -t to test configuration syntax. Organize your server blocks clearly and document domain assignments.
Related Errors
Other common Nginx errors include:
- Port conflicts: Multiple server blocks listening on the same port without proper differentiation.
- Duplicate listen directives: Overlapping
listendirectives causing binding errors. - Missing root or index: Server blocks without a root directory or index file causing 404 errors.
Key Takeaways
Each
server_name must be unique across all Nginx server blocks.Use
nginx -t to test configuration before restarting Nginx.Combine server blocks if they share the same domain to avoid conflicts.
Organize and document your Nginx configurations clearly.
Check for other conflicts like ports and listen directives to prevent errors.