Apache2.4中只允许特定IP地址或者主机访问网站

在这种情况下,我们将只允许特定的IP地址或者主机访问该网站。
其他主机和IP将无法访问Apache2.4上的网站。

<Directory "/var/www/html">
Options All
AllowOverride All
Require all denied
## 这里的"Require ip" 填写IP地址或网络
Require ip 192.168.1.4 10.9.1.1

## "Require host" 填写允许访问的主机
Require host www.example.com server01
</Directory>

也可以写成下面的格式:

<Directory "/var/www/html">
Options All
 AllowOverride All
<RequireAll>
## "Require ip" 允许访问的IP地址和网络
Require ip 192.168.1.4 10.9.1.1

## "Require host" 填写允许访问的主机
Require host www.example.com server01
</RequireAll>
</Directory>

重启apache服务

在apache配置文件中进行更改后,不要忘记重启apache服务。

如何重启apache服务:

### Ubuntu/Debian
sudo service apache2 restart

### CentOS 7/RHEL 7
systemctl restart httpd

### CentOS|RHEL 5.x,6x.
service httpd restart

Apache禁止的错误消息

当用户被拒绝访问时,页面将显示“Forbidden”。

拒绝所有人访问运行在apache2.4上的网站

Require all denied 将拒绝所有人访问网站。

<Directory "/var/www/html">
Options All
AllowOverride All
## "Require all denied" 将拒绝所有人访问本网站。
Require all denied
</Directory>

在Apache2.4中只拒绝特定IP地址或者主机访问网站

<Directory "/var/www/html">
Options All
AllowOverride All
<RequireAll>
Require all granted
## 拒绝访问的IP地址列表
Require not ip 192.168.1.4 10.9.1.1

## 拒绝访问的主机列表
Require not host www.example.com server01
</RequireAll>
</Directory>

允许所有人访问运行在Apache2.4上的网站

<Directory "/var/www/html">       
Options All
AllowOverride All
## "Require all granted" 将允许所有人访问该网站。
Require all granted
</Directory>
Apache2.4中如何限制指定的主机和IP访问

在这篇文章中,我们将了解Apache2.4中按主机和ip地址的访问控制。

Apache2.4发布了很多新特性。
在使用Apache2.4时,我们肯定会注意到访问控制的新格式。
不推荐使用allow、deny的方法,它是apache2.4版本之前的旧样式方法。
这里,我们将介绍Apache2.4中根据主机和ip地址进行访问控制。

我们将使用名为“RequireAll”的指令。
Apache 2.4文档中,Requireal指令是这样说的:

<RequireAll> and </RequireAll> are used to enclose a group of authorization directives of which none must fail and at least one must succeed in order for the <RequireAll> directive to succeed.

If none of the directives contained within the <RequireAll> directive fails, and at least one succeeds, then the <RequireAll> directive succeeds. If none succeed and none fail, then it returns a neutral result. In all other cases, it fails.

<RequireAll>和</RequireAll>用于包含一组授权指令,为了使<RequireAll>指令成功,这些指令必须至少有一个成功。

如果<RequireAll>指令中包含的指令没有一个失败,并且至少有一个成功,那么<RequireAll>指令成功。如果没有成功的,也没有失败的,那么它返回一个中立的结果。在其他所有情况下,它都失败了。
日期:2019-08-20 17:58:05 来源:oir作者:oir