Disable IP address access for Nginx in CentOS

Source: Internet
Author: User

Disable IP address access for Nginx in CentOS

We will encounter a lot of malicious IP attacks when using it. In this case, we need to use Nginx to prohibit IP Access. Next, let's take a look at the default Nginx VM accessed by the user via IP address or through an unspecified domain name (for example, someone points his own domain name to your ip address) the most important thing to take effect 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 when Nginx disables IP Access.

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.mydomain.com permanent;     } 

According to the above settings, the server cannot be accessed through an IP address, but it should appear that when server_name is followed by multiple domain names, how can one of the domain names be inaccessible? The settings are as follows:

server  {          listen 80;          server_name www.abc.com abc.com     }

Before modification, the server can be accessed through www.abc.com abc.com in server_name. After adding the Nginx settings to prohibit IP Access, the server cannot be accessed through abc.com, and www.abc.com can be accessed, if you use Nginx-t to check the configuration file, the following prompt is displayed:

   [warn]: conflicting server name “abc.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 to server {listen 80 default; server_name _; server_name www.abc.com abc.com return 500 ;}

In this way, you can access the server through abc.com.

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.