TechBuzzPro

Nginx Wrong Site? Fix Domain Config Errors

· 6 min read · Apps & Browsers

In this article
  1. How nginx Chooses Which Site to Serve
  2. Common Causes of the Wrong Site
  3. Step-by-Step Fix
  4. How to Choose Between Wildcards and Exact Names
  5. SSL and the Default Server Problem
  6. Testing and Debugging
  7. Frequently Asked Questions

If nginx shows the wrong site for your domain, the cause is almost always a misconfigured server_name or a default server block that is catching requests it shouldn't. The fix is to check your server blocks, ensure each domain has its own server_name, and set a dedicated default server that returns 444 or a redirect.

How nginx Chooses Which Site to Serve

When a request arrives, nginx looks at the Host header sent by the browser. It compares that hostname against the server_name directives in all of your server blocks. The matching process is not random — it follows a strict priority order:

  • Exact name match (e.g., server_name example.com;)
  • Longest wildcard starting with *. (e.g., *.example.com)
  • Longest wildcard ending with .* (e.g., www.*)
  • First regular expression with ~ (in order of appearance)

If no server_name matches, nginx falls back to the default server for that IP/port. The default server is either the first server block defined in the config, or the one explicitly marked with default_server in the listen directive. This is the most common reason for the wrong site appearing — your domain's server block exists, but it is not the first one, and no explicit default is set.

Common Causes of the Wrong Site

Several configuration mistakes produce the same symptom. Here are the ones to check first:

  • Missing or misspelled server_name — a typo like example.con will never match, so the request goes to the default.
  • Duplicate server blocks — if two blocks have the same server_name, nginx uses the first one, and the second is ignored.
  • No default_server defined — the first block in the config (often the one for the IP address) becomes the default, and any unmatched domain lands there.
  • Wildcard overmatching — a server_name *.example.com; will also catch sub.example.com, but not example.com itself. If you want both, you need two entries.
  • SSL certificate mismatch — with HTTPS, nginx may pick a server block based on SNI, and if the certificate doesn't cover the domain, the browser shows a warning, but the site content may still be the wrong one.

Step-by-Step Fix

Follow these steps in order. They assume you have root or sudo access to the server.

  1. List all server blocks — run grep -R "server_name" /etc/nginx/ to see every domain you have configured. Look for duplicates or obvious typos.
  2. Identify the default server — run nginx -T | grep "default_server". If nothing shows, the first block on port 80 (and port 443) is the default.
  3. Set an explicit default — in the server block you want to be the catch-all (often the IP address or a placeholder), add listen 80 default_server; (and listen 443 ssl default_server; for HTTPS). Make this block return 444 (nginx's way to close the connection) or a 301 redirect to your main domain.
  4. Ensure each domain has its own block — for each site, create a separate file in /etc/nginx/sites-available/ and symlink it to /etc/nginx/sites-enabled/. In that file, set server_name yourdomain.com www.yourdomain.com;.
  5. Test and reload — run nginx -t to check for syntax errors. If it passes, run systemctl reload nginx (or service nginx reload).

How to Choose Between Wildcards and Exact Names

Wildcards are convenient but can cause unexpected matches. For example, server_name *.example.com; matches blog.example.com and shop.example.com, but not example.com itself. If you want the bare domain too, you must list it explicitly: server_name example.com *.example.com;. Also note that wildcards only match one label — *.example.com does not match a.b.example.com. For that, you need a regular expression like ~^(.*\.)?example\.com$, but use regex sparingly because they are evaluated in order and can be slow if you have many.

SSL and the Default Server Problem

With HTTPS, the situation is more complex. nginx uses the SNI (Server Name Indication) extension from the TLS handshake to pick a server block before the HTTP request is parsed. If the SNI doesn't match any server_name, the default server is used. This means that even if your HTTP is correct, the HTTPS version might show a different site. To avoid this, set a default server for port 443 as well, and make sure it has a valid certificate (even a self-signed one will work for the default, but the browser will warn). A common practice is to have the default HTTPS block return 444 or redirect to a known domain.

Testing and Debugging

After making changes, verify the fix with curl using the Host header:

curl -H "Host: yourdomain.com" http://your-server-ip/

This sends a request that mimics a browser visiting yourdomain.com but goes directly to the IP. If you get the correct page, the config is right. If you get the default page, your server_name still doesn't match. Also check the nginx error log (/var/log/nginx/error.log) for messages about "no host in request line" or "server name not found". Another useful tool is nginx -T which prints the entire effective configuration — you can see exactly which server block is the default and what server_name each one has.

Frequently Asked Questions

Why does nginx show the first site even when I type the second domain?
Because the first server block in the config is the default when no default_server is specified. Your second domain's server_name may be correct, but if the request doesn't match it (e.g., you typed a different subdomain), it falls back to the default. Add default_server to a dedicated block and give it a catch-all server_name _;.

What does server_name _; mean?
It's a convention for a catch-all server block. The underscore is not a special wildcard — it's just an invalid hostname that never matches a real domain, so nginx uses this block only when no other server_name matches. It's a clean way to define a default.

I have two domains pointing to the same IP, but nginx shows the same site for both. Why?
You likely have only one server block with a server_name that matches one domain, and no block for the other. Or you have two blocks but both have the same server_name. Create a separate block for each domain, each with its own server_name and document root.

Does the order of server blocks matter?
For exact matches, no — nginx picks the exact match regardless of order. For wildcards and regex, order matters. The first matching wildcard wins, and regexes are tested in the order they appear. Also, the first block (or the one with default_server) is the default for unmatched requests.

Why does my HTTPS site show the wrong certificate?
Because nginx picks the server block for HTTPS based on SNI, and if the SNI doesn't match, it uses the default. If your default block has a certificate for a different domain, that's what the browser sees. Set a default HTTPS block with a certificate that covers your main domain, or use a single certificate with SANs for all your domains.

Can I use a regex in server_name to match multiple subdomains?
Yes, but only with the ~ prefix. For example, server_name ~^(www\.)?(.*)\.example\.com$; matches any subdomain. However, regex is slower and harder to debug, so use it only when wildcards are insufficient. Also remember that regex is tested after all exact and wildcard matches, so it won't override them.