Two methods for Nginx or Apache to prohibit access to certain IP segments


Modify the Nginx configuration file nginx.conf
Nginx configuration access IP can modify the nginx.conf file, just add allow and deny IP in the server, as follows:

server {     listen 80;     server_name localhost;     allow all;     deny 123.123.123.123;     error_page 500 502 503 504 /50x.html; } Note! In the above configuration, allow must be configured before deny, otherwise allow will not take effect.






 

Allowing or denying an IP access in Nginx is written like this:

allow 192.168.0.1;
deny 192.168.0.2;
 
If you need to disallow access to the entire IP segment, you can write:

deny 123.0.0.0/8; // block the ip of 123.0.0.1~123.255.255.254
deny 123.123.0.0/16; // block the ip of 123.123.0.1~123.123.255.254
deny 123.123.123.0/24; / / Seal 123.123.123.1~123.123.123.254 ip
deny all; // Seal all ip
 
instructions, allow is the same as deny configuration, if you need to open a certain IP segment, just change the above deny to allow.

In addition, it should be noted that the configuration modification needs to restart the Nginx server to take effect.

Modifying the website configuration file.htaccess
In addition to modifying the Nginx configuration file, you can also restrict access to a certain IP (segment) by modifying the website configuration file.htaccess. The method is as follows.

Save the following content as a .htaccess file and upload it to the root directory of the website. If the .htaccess file already exists, add the following content to the beginning of the file.

Order allow, deny
allow from all
deny from 123.123.123.123
 
If you want to limit an IP segment, such as 123.123.123.* this C segment, enter:

Order allow, deny
allow from all
deny from 123.123.123
 
The above content means that all IPs starting with 123.123.123 are rejected, and other IPs are allowed.

If you want to limit multiple IP segments, separate them with spaces, such as:

Order allow, deny
allow from all
deny from 111.111.111 111.222.222 111.333.333
 
indicates that the configuration does not need to restart the web server, and the modification takes effect immediately.

In addition, the method is applicable to any web sites, such as IIS, Apache sites. 


 

Guess you like

Origin blog.csdn.net/happyzhlb/article/details/130583572