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

Source: Internet
Author: User
: This article describes how to prohibit Nginx from directly using IP addresses or unbound domain names to access the Web server. For more information about PHP tutorials, see. Today, we need to set up on Nginx to prohibit access to the server through IP addresses, but only through domain names. this is done to prevent unauthorized domain names from being resolved to their own server IP addresses, resulting in server network disconnection, find the following solutions on the network:

Nginx's default virtual host takes effect when the user accesses it through an IP address or through an unspecified domain name (for example, someone points his own domain name to your ip address, the most important thing is to add this line in the server settings:

Listen 80 default; # The following default parameter indicates that this is the default virtual host.

This setting is very useful.
For example, if you want to disable the display of any valid content when someone accesses your website through an ip address or an unknown domain name, you can return 500 to someone else.
At present, many data centers in China require the website owner to disable the empty host header to prevent troubles caused by domain names not filed. You can set it as follows:
Server {
Listen 80 default;
Return 500;
}

You can also collect the traffic and import it to your website. you only need to perform the following redirect settings:
Server {
Listen 80 default;
Rewrite ^ (. *) http://www.linuxidc.com permanent;
}
====================================

According to the above settings, the server cannot be accessed through an IP address. However, when server_name is followed by multiple domain names, one of them cannot be accessed:

The settings are as follows:
Server
{
Listen 80;
Server_name www.linuxidc.com linuxidc.com
Before modification, you can access the server through www.linuxidc.com linuxidc.com in server_name. after adding the settings to prohibit IP access, you cannot access the server through linuxidc.com and www.linuxidc.com.

If you use nginx-t to check the configuration file, the following prompt is displayed:

[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, the solution is solved by adding server_name _; after listen 80 default;. the format is as follows:

# Prohibit IP access
Server
{
Listen 80 default;
Server_name _;

Server_name www.linuxidc.com linuxidc.com

Return 500;
}
Or
Server {
Listen 80 dufault;
Server_name _;

Server_name www.linuxidc.com linuxidc.com

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

Nginx forwarding:
First case: Access Site A and direct it to site B
Server {
Server_name www.linuxidc.net;
Rewrite ^ (. *) http://www.linuxidc.com $1 permanent;
}
Case 2: not redirect all accesses to the specified page
Server {
Server_name www.linuxidc.net;
If ($ host! = 'Linuxidc. net '){
Rewrite ^/(. *) $ http://www.linuxidc.com/#1 permanent;
}
}
If it is written in the first server segment
IP address access will also be redirected


But there is still a problem. for some special addresses, I need to use ip addresses for access. 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.

Server {
Listen 80 default;
Server_name _;
Location/xxxxx {
Stub_status on;
Access_log off;
}
Location /{
Rewrite ^ http://www.domain.com $ request_uri ?;
}
}

In this way, the functions we want are implemented.

The above section describes how to prohibit Nginx from directly using IP addresses or unbound domain names to access the Web server, including some content, and hope to help friends who are interested in PHP tutorials.

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.