How to Change Nginx Default Page Status Code from 200 to 404


2 views

When Nginx can't find a matching server block (virtual host) for an incoming request, it falls back to the default server. This default server typically serves a static HTML page with the message "Welcome to nginx!" and returns an HTTP 200 OK status code. For many use cases, especially in production environments, you might want this scenario to return a 404 Not Found instead.

Returning a 200 OK for unmatched domains can cause SEO issues (search engines might index duplicate content) or security concerns (attackers could discover unused domains). A 404 response is more appropriate for requests that don't match any configured virtual host.

Here's how to modify the default server configuration in nginx.conf or your site configuration file:


server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    
    # Return 404 with custom HTML (optional)
    return 404;
    
    # Alternative: Serve default page but with 404 status
    # location / {
    #     root /usr/share/nginx/html;
    #     index index.html;
    #     error_page 404 = /index.html;
    # }
}

After making changes:


sudo nginx -t        # Test configuration
sudo systemctl reload nginx

Verify with:


curl -I http://unconfigured-domain.com

You should now see:


HTTP/1.1 404 Not Found

If you want to keep showing the default page but with a 404 status:


server {
    listen 80 default_server;
    server_name _;
    root /usr/share/nginx/html;
    
    location / {
        try_files $uri $uri/ =404;
        error_page 404 /index.html;
    }
}

When Nginx can't match an incoming request to any configured virtual host, it serves the default "Welcome to nginx!" page with a 200 HTTP status code. While this is the standard behavior, many system administrators prefer to return a 404 status code for unmatched hosts to:

  • Improve security by not confirming the server's identity
  • Better align with HTTP standards (since no valid resource exists)
  • Prevent accidental indexing of default pages by search engines

Here's the most straightforward way to implement this change in your nginx.conf or default server configuration:


server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    
    location / {
        root /var/www/html;
        try_files $uri $uri/ =404;
        
        # This will serve the default nginx page but with 404 status
        error_page 404 /404.html;
        return 404;
    }
}

If you want to maintain the default page content but ensure it's served with a 404 status:


server {
    listen 80 default_server;
    server_name _;
    
    location / {
        root /usr/share/nginx/html;
        index index.html;
        
        # First try to serve the request normally
        try_files $uri $uri/ @notfound;
    }
    
    location @notfound {
        # Serve default nginx page but with 404 status
        root /usr/share/nginx/html;
        rewrite ^ /index.html break;
        return 404;
    }
}

After implementing either solution, verify it works correctly:


curl -I http://your-server-ip/

Should return:


HTTP/1.1 404 Not Found
...

When implementing this change:

  • Make sure default_server is properly set on the listen directive
  • The root path must point to where your default index.html is located
  • Consider adding a custom 404 page for better user experience
  • Test with both domain and IP address requests

If the status code doesn't change as expected:

  1. Check for multiple default_server declarations
  2. Verify the server_name _ directive is present
  3. Ensure no other server blocks are catching these requests
  4. Check Nginx error logs for configuration issues