During website launch or business integration, many website owners resolve multiple domains to the same server simultaneously to better allocate traffic, differentiate business entry points, or implement SEO strategies. However, when multiple domains share the same IP, the most common problems arise: some domains are accessible normally while others redirect to the default site? New domains are not recognized by Nginx after resolution? Domain A displays the website normally, but domain B displays the Nginx default page? Even though DNS resolution is successful, the browser still displays a 404 or error site? The root cause of these problems is often not resolution failure, but rather a lack of synchronization between the DNS and Nginx host configurations.
I. Why do anomalies occur when multiple domains resolve to the same IP?
1. DNS is only responsible for pointing domain names to server IPs; it doesn't care how your server internally processes these domain names.
For example:
A record:
A Record:
www.a.com → 1.2.3.4
www.b.com → 1.2.3.4
www.c.com → 1.2.3.4
From a DNS perspective: All three domains successfully point to the server, no problem.
2. Internally, Nginx determines which site responds. When multiple domains access the same IP, Nginx decides which site to return based on the following order:
Nginx site matching priority:
- Virtual host matching the complete `server_name`
- Wildcard match or regular expression match
- `default_server` (default site)
If your domain is not specified in the corresponding `server_name`, accessing that domain will result in the default site, leading to: displaying the Nginx test page, accessing a different website, encountering 403/404 errors, or being redirected to a different domain's website.
Therefore, the root cause of multi-domain resolution errors is almost always: DNS resolution has succeeded, but Nginx is not configured accordingly.
II. Three Typical Manifestations of DNS and Nginx Inconsistency
1. DNS resolution is normal, but accessing the website displays the Nginx default page. This indicates that the request reached the server, but the domain name was not correctly assigned to the corresponding server block.
2. DNS resolution is delayed, and Nginx still returns the old site content. This is because DNS has a cache; even if you change the resolution, it may take several minutes to several hours for it to take effect globally.
3. Multiple domain names resolve to the same IP, but the displayed content is disordered. For example: accessing a.com displays b.com content, accessing b.com displays the default site, and accessing c.com returns a 403/404 error. This is a typical case of server_name not configured or a configuration conflict.
III. Verification Methods to Ensure DNS is Normal
1. Use dig or nslookup to check if the DNS resolution is effective.
dig a.com
dig b.com
dig c.com
1. Confirm that all responses return the same IP address.
2. Use ping to test if the correct IP address is resolved.
ping a.com
ping b.com
ping c.com
If all three domain names resolve normally, then DNS issues are ruled out.
3. If DNS entries are added or modified, you must wait for them to take effect.
Some DNS TTL values are 300-600 seconds, and some are even longer.
It is normal for it to take some time for the changes to take effect.
IV. How to Correctly Configure Multiple Domain Names in Nginx (Core Solution)
Solution 1: Multiple domain names pointing to the same website (most common)
For example: a.com, www.a.com, b.com, and www.b.com all open the same website.
Then the Nginx configuration is as follows:
server {
listen 80;
server_name a.com www.a.com b.com www.b.com;
root /var/www/site;
index index.html index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
}
The key points are:
`server_name a.com www.a.com b.com www.b.com;`
All domain names must be included; otherwise, Nginx will not recognize them.
Option Two: Bind multiple domain names to different websites.
For example:
a.com → Website A
b.com → Website B
c.com → Website C
Then the configuration must be divided into multiple server blocks:
server {
listen 80;
server_name a.com www.a.com;
root /var/www/siteA;
}
server {
listen 80;
server_name b.com www.b.com;
root /var/www/siteB;
}
server {
listen 80;
server_name c.com www.c.com;
root /var/www/siteC;
}
Remember:
Each domain name can only appear in one server block.
V. Complete Nginx Multi-Domain Troubleshooting Process (Most Practical Part)
No matter what problem you encounter, you can almost always solve it by following these steps:
Step 1: Confirm that the DNS is resolving to the correct IP address.
dig yourdomain.com
ping yourdomain.com
Step 2: Confirm whether Nginx recognizes the domain name.
curl -I yourdomain.com
If the return value is not as expected, proceed to the next step.
Step 3: Find all server_name configurations
Usage:
grep -R "server_name" /etc/nginx/sites-enabled/
Confirm that the domain name has actually been entered.
Step 4: Check if it falls into default_server
Run:
nginx -T | grep default_server -n
Check which server block is preempting the default.
Step 5: Check the error.log output.
tail -f /var/log/nginx/error.log
If the following occurs:
no host in upstream
misdirected request
This indicates a configuration conflict.
In summary: When multiple domains are bound to the same server, issues such as DNS resolution errors, access problems, and redirection to the default site are not rooted in the DNS itself, but rather in a situation where the DNS is functioning correctly but Nginx is not configured correctly. By following the troubleshooting methods described in this article, almost all multi-domain resolution problems can be resolved in one go.
CN
EN