Nginx prohibits direct access to Web servers using IP or unbound domain names

Source: Internet
Author: User
Today to set the forbidden on the Nginx IP access to the server, only through the domain name access, this is to avoid others to the non-filing of the domain name resolution to their server IP and cause the server is disconnected from the network, the following solutions:

Nginx Default virtual host when the user through the IP access, or through the non-set domain access (for example, someone has his own domain name pointing to your IP), the most important thing is to add this line in the server settings:

Listen default; #后面的default参数表示这个是默认虚拟主机.

This setting is very useful.
For example, when someone accesses your website via IP or unknown domain name, you want to suppress any valid content, you can return 500.
At present, many computer rooms are required to shut down the main web site host head, to prevent the non-filing of the domain name pointing over the cause of trouble. You can set it like this:
server {
Listen default;
return 500;
}

You can also collect these traffic, import to your own website, just do the following jump settings can be:
server {
Listen default;
Rewrite ^ (. *) http://www.linuxidc.com permanent;
}
==============================

After setting up as above, it is really not possible to access the server via IP, but in the case of server_name followed by multiple domain names, one of the domains cannot be accessed:

Settings are as follows:
Server
{
Listen 80;
server_name www.linuxidc.com linuxidc.com
Before the change, through the www.linuxidc.com in server_name Linuxidc.com can access the server, add the Prohibit IP access settings, through the linuxidc.com cannot access the server, www.linuxidc.com can access

Using NGINX-T to detect the configuration file will prompt warning:

[Warn]: Conflicting server name "linuxidc.com" on 0.0.0.0:80, ignored
The configuration file/usr/local/webserver/nginx/conf/nginx.conf syntax is OK
Configuration file/usr/local/webserver/nginx/conf/nginx.conf test is successful

Finally through the listen in the default, and then add server_name _, solve the form as follows:

#禁止IP访问
Server
{
Listen default;
server_name _;

server_name www.linuxidc.com linuxidc.com

return 500;
}
Or
server {
Listen Dufault;
server_name _;

server_name www.linuxidc.com linuxidc.com

Rewrite ^ (. *) http://www.linuxidc.net permanent;
}
In this way, through the linuxidc.com can access the server, the problem is solved, but the specific reasons are unclear.

Nginx Forwarding:
The first case: Access a station directed to the B station
Server {
server_name www.linuxidc.net;
Rewrite ^ (. *) http://www.linuxidc.com$1 permanent;
}
The second case: not all redirects to the a station to the specified page
server {
server_name www.linuxidc.net;
if ($host! = ' linuxidc.net ') {
rewrite ^/(. *) $ http://www.linuxidc.com/$1 permanent;
}
}
If written in the first server segment
Use IP access will also be redirected


But there's still a problem, some special address, I need to use IP access, the others are forbidden, how to configure it? For example, I want to let the surveillance treasure directly with the IP access to my machine's nginx status information, and other IP access to all requests to jump to the domain name.

server {
Listen default;
server_name _;
location/xxxxx{
Stub_status on;
Access_log off;
}
Location/{
Rewrite ^ Http://www.domain.com$request_uri?;
}
}

This will enable the functionality we want.

Above describes the Nginx prohibit directly using IP or unbound domain names to access the Web server, including the content of the contents, I hope that the PHP tutorial interested in a friend helpful.

  • 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.