keepcalculator

06 Numbers and Powers

Set a Default Nginx Site for Unmatched Domains

Card No. 06·70516 · Updated · Standard formula, shown below

This guide shows you how to configure Nginx so that any domain or IP address that does not match a configured server block is served by a specific default site. You’ll learn to set up a catch-all server block that handles unmatched requests, preventing errors and ensuring visitors see a predictable page.

What You’ll Need

  • A server with Nginx installed and running (any recent version).
  • Root or sudo access to edit Nginx configuration files.
  • Basic familiarity with the command line and Nginx configuration syntax.

Step-by-Step

  1. Locate your Nginx configuration directory. Typically, this is /etc/nginx/. The main configuration file is nginx.conf, and site-specific configurations are usually in sites-available/ and sites-enabled/ (Debian/Ubuntu) or directly in conf.d/ (RHEL/CentOS). Check your setup to know which applies.
  2. Create a new server block file for the default site. Use a text editor to create a new file, for example /etc/nginx/sites-available/default. If you already have a default file, you can edit it, but it’s cleaner to create a dedicated one.
  3. Write the server block configuration. In the new file, add a server block with listen 80 default_server; (and optionally listen [::]:80 default_server; for IPv6). This directive tells Nginx to use this block for any request that doesn’t match another server block’s server_name. For HTTPS, add listen 443 ssl default_server; and include your SSL certificate paths. A minimal example:
    server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name _;
        root /var/www/default;
        index index.html;
    }

    The server_name _; is a common convention for a catch-all, though it’s not strictly required when using default_server.

  4. Create the default web root. Create the directory specified in the root directive, e.g., sudo mkdir -p /var/www/default. Then add an index.html file with some content, like a simple “This site is not configured” message.
  5. Enable the server block. If you’re using sites-available and sites-enabled, create a symbolic link: sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/. If you’re using conf.d/, just placing the file there is enough.
  6. Test the configuration. Run sudo nginx -t to check for syntax errors. If successful, you’ll see a message like “syntax is ok” and “test is successful”.
  7. Reload Nginx. Apply the changes with sudo systemctl reload nginx (or sudo service nginx reload on older systems).
  8. Verify. Visit any domain that points to your server’s IP but isn’t configured in another server block. You should see the default page you created. Also, test an existing site to ensure it still works correctly.

Common Mistakes

  • Forgetting default_server: Without this directive, Nginx will use the first server block as the default, which may not be the one you intend.
  • Not enabling the site: In Debian/Ubuntu, forgetting to symlink the file to sites-enabled means the block is ignored.
  • Conflicting listen directives: If another server block also has default_server, Nginx will fail to start or use the first one encountered. Ensure only one block has this directive per port.
  • SSL certificate issues: For HTTPS default sites, using a self-signed certificate will cause browser warnings, which is expected. If you use a real certificate, it must cover the domain being requested, which is impossible for unmatched domains, so a self-signed cert is common.

Tips

  • Use a dedicated default site that returns a 404 or a simple maintenance page. This is better than showing an error or a random site.
  • Log unmatched requests to a separate access log to monitor suspicious traffic or typos. Add access_log /var/log/nginx/default_access.log; to the server block.
  • Consider returning a 444 status code (no response) for unmatched requests to drop connections from bots scanning for unconfigured domains. Add return 444; in the server block.
  • For HTTPS, you can use a single self-signed certificate for the default server, but be aware that browsers will show a warning. To avoid warnings, you might need to use a wildcard certificate if you own the domain, but that’s not typical for a catch-all.

Troubleshooting

  • Nginx fails to start: Run sudo nginx -t to see the error. Common issues include duplicate default_server directives or syntax errors in the config file.
  • Default site not showing: Check that the default_server directive is present and that no other server block has it. Also, verify that the file is in the correct directory and enabled (if using sites-enabled).
  • Existing sites break: Ensure that your other server blocks have explicit server_name directives. If they don’t, Nginx might route requests to the default site instead. Add server_name to each block.
  • HTTPS requests go to the wrong site: If you have multiple SSL certificates, Nginx uses SNI to match the domain. For unmatched domains, it will use the default server. Make sure your default server has an SSL certificate configured, even if self-signed, to avoid errors.
  • Changes not taking effect: After editing, always run sudo nginx -t and then reload. If you see “test failed”, fix the errors before reloading.

Frequently Asked Questions

What is the difference between default_server and server_name _;?

default_server is a flag on the listen directive that makes that server block the default for the specified port. server_name _; is just a name that doesn’t match any real domain; it’s a convention, but the default_server flag is what actually makes it the catch-all.

Can I have a default site for both HTTP and HTTPS?

Yes, you need to include listen 80 default_server; and listen 443 ssl default_server; in the same server block, along with your SSL certificate paths. This way, unmatched requests on both ports go to the same site.

Will this affect my existing websites?

No, as long as your existing server blocks have a server_name that matches the domain they serve. The default server only handles requests that don’t match any other block’s server_name.

How do I test without changing DNS?

You can use the curl command with the Host header to simulate a request to an unmatched domain: curl -H "Host: unknown.example.com" http://your-server-ip. This will show you what the default site returns.

What should I put in the default site’s root?

Typically, a simple HTML page that says “Site not found” or a redirect to a main site. You could also use a 404 status code. Some admins prefer to return 444 to drop the connection entirely.

Can I use a variable in the root path?

Yes, you can use variables like $host in the root directive, but that can be a security risk if not handled carefully. It’s safer to use a static path for the default site.

Related

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

More cards in this drawer

The whole Numbers and Powers drawer →