By default, Nginx allows you to access a web site directly using an IP address, or through an unconfigured domain name (such as someone pointing their own domain name to your server’S IP address). So how do we set up Nginx to disable this behavior?

server {
    listen 80 default_server;
    server_name _;
    return 404;
}Copy the code

For the unbound domain name pointing to your server, can not match the virtual host domain name configured by you, will use the default virtual host, and then directly return 404.

Listen 80 default_server: Specifies the default host of port 80 in the configuration segment of the server. That is, if the unbound domain name points to your server and fails to match the configured virtual host name, the virtual host is used by default.

Server_name _: The _ can be replaced with any invalid character or an invalid domain name, indicating that the server configuration cannot be accessed.

Return 404: a 404 error is directly returned.