Various methods to prohibit IP addresses from accessing websites (linux, php, nginx, and apache)

Source: Internet
Author: User

PHP disables access from an IP address or IP address segment

Not much nonsense. For more information, see:

The code is as follows: Copy code
<?
// Disable an IP address
$ Banned_ip = array (
"127.0.0.1 ",
// "119.6.20.66 ",
"192.168.1.4"
);
If (in_array (getenv ("REMOTE_ADDR"), $ banned_ip ))
{
Die ("Your IP address is forbidden to access! ");
}
// Disable an IP segment
$ Ban_range_low = ip2long ("119.6.20.65 ");
$ Ban_range_up = ip2long ("119.6.20.67 ");
$ Ip = ip2long ($ _ SERVER ["REMOTE_ADDR"]);
If ($ ip> $ ban_range_low & $ ip <$ ban_range_up)
{
Echo "your IP address is in the prohibited IP segment. Access is prohibited! ";
Exit ();
}
?>

Apache prohibits IP access

Method 1: Add the following code at the end of the httpd. conf file:

The code is as follows: Copy code
NameVirtualHost 221 .*.*.*
<VirtualHost 221. *>
Server name 221 .*.*.*
<Location/>
Order Allow, Deny
Deny from all
</Location>
</VirtualHost>
<VirtualHost 221. *>
DocumentRoot "c:/web"
ServerName www.111cn.net
</VirtualHost>

Note: the blue part is used to reject the request directly through 221. *. *. * for any access request from this IP address, if you use 221. *. *. * access is denied. The red part allows access through the domain name www.111cn.net. The main directory points to c:/web (assume that the root directory of your website is c:/web)

Linux shield IP access

The code is as follows: Copy code

# The command to shield a single IP address is
Iptables-I INPUT-s 123.45.6.7-j DROP
# The Command for sealing the entire segment from 123.0.0.1 to 123.20.255.254
Iptables-I INPUT-s 123.0.0.0/8-j DROP
# An IP address segment is a command from 123.45.0.1 to 123.45.255.254.
Iptables-I INPUT-s 124.45.0.0/16-j DROP
# The Command from 123.45.6.1 to 123.45.6.254 is
Iptables-I INPUT-s 123.45.6.0/24-j DROP
Service iptables save is saved in/etc/sysconfig/iptables. If no iptables file exists, it is automatically created.

Nginx disables IP access

First, create the following configuration file and place it under the nginx conf directory, and name it blocksip. conf:

The code is as follows: Copy code
Deny 4.4.4.4 // This is the IP address to be disabled by nginx

Save it.
Add: include blocksip. conf to the nginx configuration file nginx. conf, and restart nginx to take effect. When the blocked ip address opens the site, the following message is displayed:
403 Forbidden

Blocksip. conf: There are many other formats. You can configure only allowed IP access or IP segment access:

The code is as follows: Copy code
Deny IP;
Allow IP;
# Block all ips
Deny all;
# Allow all ips
Allow all;

The CIDR block format is as follows: 192.168.1.0/24.

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.