If we need to block an IP address or an IP segment in our VPS and server, we can directly add it to the Nginx and Apache rules. This usage is often useful, for example, it is not allowed to access a user's IP address or spider crawling information, or even some collection and image IP addresses need to be blocked. Although this function has not yet been used by Lao Jiang, it has been recorded by some netizens and can be directly used if necessary.
1. Apache environment
If the server is an Apache environment, add the following file directly to the. htaccess file in the current root directory.
The code is as follows: |
Copy code |
Order allow, deny
Deny from 220.116.0.0 # shield an IP address
Deny from 220.116.0.0/14 # Blocked IP segments
Allow from all
|
Directly add it to the. htaccess file in our root directory. It takes effect immediately without restarting.
II. Nginx environment
Nginx is slightly different. You need to use a blocking IP list file (blockip. conf) and then add a blocking IP address and IP segment.
The code is as follows: |
Copy code |
Deny 220.116.0.0; // block an IP address
Deny 220.116.0.0/14; // block the IP address segment
|
Then edit the nginx. conf configuration file.
The code is as follows: |
Copy code |
Include blockip. conf;
|
This file can be added in http {}, server {}, but note the relative path. We can place blockip. conf in a directory with the Nginx. conf file. Of course, we can also shield a single website IP address from the conf file of the current website.
Restart Nginx to take effect.