ThisCalculator

Everyday

Nginx Welcome Page on www but Not Bare Domain

Updated · Standard formula, shown below

Nginx Welcome Page on www but Not Bare Domain

When Nginx shows the welcome page on www.example.com but not on example.com, the cause is almost always a missing or misconfigured server block for the bare domain. The welcome page appears because Nginx falls back to its default server when no matching server_name is found, so your bare domain is being served by the default configuration instead of your site.

Background: How Nginx Chooses a Server Block

Nginx uses the Host header sent by the browser to decide which server block (virtual host) should handle a request. When you type example.com, the browser sends Host: example.com. Nginx compares this against the server_name directives in all server blocks. If no block has a matching server_name, Nginx uses the first server block defined in the configuration (or the one marked with default_server). On a fresh installation, that default block is the welcome page located at /usr/share/nginx/html/index.html (or similar).

If your www subdomain works, it means you have a server block that explicitly includes server_name www.example.com;. But if the bare domain block is missing or its server_name is misspelled, Nginx treats it as unmatched and serves the default page.

How the Default Server Works

The default server is the one that handles requests when no server_name matches. It is either the first server block in the configuration file order or the one with the default_server parameter in the listen directive. For example:

server {
    listen 80 default_server;
    server_name _;
    root /usr/share/nginx/html;
}

This block catches all requests that don’t match any other block. If your bare domain doesn’t match any server_name, it lands here and you see the welcome page.

Common Causes and Fixes

Here are the typical reasons why the bare domain fails while www works:

  • Missing server block for the bare domain. You may have only defined server_name www.example.com;. Add another block or include both names in the same block: server_name example.com www.example.com;.
  • Typo in the domain name. Check for extra dots, missing letters, or a trailing dot. Nginx matches exactly, so example.com is different from example.com. (with a trailing dot).
  • Wrong file or symlink. On Debian/Ubuntu, server blocks are in /etc/nginx/sites-available and symlinked into sites-enabled. If the symlink is missing for the bare domain block, it won’t be loaded.
  • DNS or redirect issue. Sometimes the bare domain DNS record points to a different server or an external redirect sends you to the default page. Use dig example.com or nslookup to verify the IP address.

To fix it, edit your Nginx configuration (usually in /etc/nginx/sites-available/your-site) and ensure the server_name includes both domains. Then test and reload:

  1. sudo nginx -t to check syntax.
  2. sudo systemctl reload nginx (or service nginx reload).
  3. Clear your browser cache or use incognito mode.

How to Choose Between www and Non-www

You can pick one canonical domain and redirect the other to it. This avoids duplicate content and cookie issues. For example, to redirect www to the bare domain, add this server block:

server {
    listen 80;
    server_name www.example.com;
    return 301 http://example.com$request_uri;
}

Then your main block handles example.com. Alternatively, do the opposite if you prefer www. The choice is mostly about branding and cookie scope; both are valid. Just be consistent.

Common Problems and Misconceptions

One common misconception is that Nginx automatically treats www and the bare domain as the same. It does not. They are completely different hostnames unless you explicitly configure both. Another issue is that some tutorials show server_name _; as a catch-all, but that only matches when no other block matches, so it doesn’t help if your bare domain is supposed to be handled by a specific block.

Another problem: if you use SSL certificates, you need a certificate that covers both names (e.g., a wildcard or a SAN certificate). If your certificate only covers www.example.com, the bare domain will show a certificate error, which may cause the browser to fall back to a default page or show a warning.

How to Test and Debug

You can test which server block is being used by checking the response headers or using curl:

curl -I http://example.com
curl -I http://www.example.com

Look at the Server header and the content. If the bare domain returns the welcome page, the response body will contain Welcome to nginx!. You can also enable Nginx’s debug log by adding debug_points abort; to the config, but that’s rarely needed.

Check the Nginx error log (/var/log/nginx/error.log) for clues like “no host in request” or “server name not found”. Also verify that the configuration file is actually loaded by running nginx -T to dump the full effective configuration.

Alternatives to Fixing the Config

If you can’t edit the Nginx config directly (e.g., on a shared host), you might be able to use a control panel like cPanel or Plesk to add the bare domain as an alias. Some hosting providers automatically handle both, but if not, you may need to contact support. Another alternative is to use a redirect service at the DNS level, but that often breaks SSL and is not recommended.

Frequently Asked Questions

Why does the welcome page appear only on the bare domain and not on www?

Because Nginx has a server block that matches www.example.com but no block that matches example.com. The bare domain falls back to the default server, which serves the welcome page.

How do I make both www and non-www work?

Add both names to the server_name directive in your server block, or create two blocks: one for the main domain and one that redirects the other to it. Then reload Nginx.

Is it a DNS issue or an Nginx issue?

It could be either. First check that both domains resolve to the same IP with dig or nslookup. If they do, it’s almost certainly an Nginx configuration issue.

Can I use a wildcard server_name?

Yes, server_name *.example.com; matches any subdomain, but it does not match the bare domain. You need to include the bare domain explicitly or use a regex like server_name ~^(www\.)?example\.com$;.

What if I use HTTPS? Does the same apply?

Yes, the same logic applies, but you also need a valid SSL certificate that covers both names. If the certificate doesn’t match, the browser will block the request before Nginx even serves the page.

Why does the welcome page still show after I fixed the config?

You may need to restart Nginx instead of just reloading, or the browser may be caching the old page. Try a hard refresh or clear the cache. Also verify that the config file you edited is the one actually loaded by running nginx -T.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.