How to implement nginx forbidding IP access

Time:2021-3-11

Nginx forbids IP access

We will encounter a lot of malicious IP attacks when we use it. At this time, we will use nginx to prohibit IP access. Now let’s take a look at nginx’s default virtual host, which takes effect when a user accesses through IP or through an unset domain name (for example, someone points his own domain name to your IP). The most important thing is to add this line to the server’s settings:

listen 80 default;

The following default parameter indicates that this is the default virtual host.

Nginx forbids IP access. This setting is very useful.

For example, when someone visits your website through IP or unknown domain name, you want to prohibit the display of any valid content, and you can return 500 to him. At present, many computer rooms in China require the website owner to close the empty host header, so as to prevent the unregistered domain name from pointing to cause trouble. It can be set as follows:

server { listen 80 default; return 500; }

You can also collect the traffic and import it to your website. Just do the following jump settings:

server { listen 80 default; rewrite ^(.*) http://www.mydomain.com permanent; }

According to the above settings, the server can’t be accessed through IP, but it should be used as a server_ When name is followed by multiple domain names, one of them cannot be accessed. The settings are as follows:

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

Before the change, through the server_ In name www.abc.com abc.com Can access the server, join the nginx prohibit IP access settings, through the abc.com Unable to access the server, www.abc.com It can be accessed. Detecting the configuration file with nginx – t will prompt warning:

lastWebmaster blogBy adding server after listen 80 default_ name _ The solution is as follows:

#Prohibit IP access to server {listen 80 default; server}_ name _ ; server_ name www.abc.com abc.com return 500; }

So, through abc.com You can access the server.

Thank you for reading, hope to help you, thank you for your support!