Access to unbound domain names is prohibited on the nginx host

Source: Internet
Author: User
Tags http request regular expression nginx host

Add the following server segment to the nginx configuration file nginx. conf.

The code is as follows: Copy code

Server {
Listen 80 default_server;
Server_name _;
Return 404;
}


Or

Server {
Listen 80 default_server;
Server_name _;
Return 444;
}

The nginx virtual Host finds the corresponding virtual Host configuration through the Host value in the HTTP request. What if it cannot be found? Then nginx sends the request to the node with the default_server specified for processing.

Through the above configuration, when an unbound domain name points to your server, the above virtual host will be used by default because it does not match the configured virtual host domain name, then, the system directly returns 404, which disables access to the unbound domain name.

As for why server_name is _, it is actually only a representative of many invalid domain names, and it will never overlap with any real name. Other invalid names, such as "-" and "! @ # "Can also be used.

In this case, there is still a problem. For some special addresses, I need to use ip addresses to access them. Others are not allowed. How can I configure them? For example, if I want the monitoring site to directly access the nginx status information of my machine using an ip address, all other requests accessed using an ip address will jump to the domain name.

The code is as follows: Copy code
Server {
Listen 80 default_server;
Server_name _;
Location/xxxxx {
Stub_status on;
Access_log off;
}
Location /{
Rewrite ^ http://www.111cn.net $ request_uri ?;
}
}

Use Nginx-t to check the configuration file!

In addition, let's talk about server_name here. Server_name can use regular expressions. This function is quite practical.

The server_name command in Nginx is mainly used to configure name-based virtual hosts. The server_name command matches the following order after receiving the request:

1. Accurate server_name matching, for example:

The code is as follows: Copy code
Server {
Listen 80;
Server_name 111cn.net www.111cn.net;
...
}

2. String starting with * wildcard:

The code is as follows: Copy code
Server {
Listen 80;
Server_name * .111cn.net ;...
}

3. String ended with a * wildcard:

The code is as follows: Copy code
Server {
Listen 80;
Server_name www .*;
...
}

4. Regular expression matching:

The code is as follows: Copy code
Server {
Listen 80;
Server_name ~ ^ (?. +) .Domain.com $ ;...
}

Nginx will match the server name in the order of 1, 2, 3, 4. The search will be stopped only after one match, therefore, when using this command, we must clearly identify the matching sequence (similar to the location command ).

A very practical function of the server_name command is to use the regular expression capture function, which can streamline the configuration file as much as possible. After all, it is inconvenient to maintain the configuration file too long. The following are two specific applications:
1. Configure multiple sites in one server block:

The code is as follows: Copy code

Server
{
Listen 80;
Server_name ~ ^ (Www .)? (. +) $;
Index. php index.html;
Root/data/wwwsite/$2;
}
The main directory of the site should be similar to the following structure:
/Data/wwwsite/111cn.net
/Data/wwwsite/nginx.org
/Data/wwwsite/baidu.com
/Data/wwwsite/google.com

In this way, you can use only one server block to complete the configuration of multiple sites.


Specifies the page for redirecting an unauthorized domain name

Change return 403; (return error 403);

The code is as follows: Copy code

Rewrite ^ (. *) http://www.111cn.net/stop.html permanent;

Restart nginx.

/Usr/local/nginx/sbin/nginx-s reload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.