仅允许从一个 IP 地址访问 Apache 服务器

仅允许从一个 IP 地址访问 Apache 服务器

我有一台 Apache 服务器,目前我需要阻止除特定人群之外的所有人访问。我认为最简单的方法是拒绝所有流量的访问,然后仅允许少数 IP 地址访问。从我在网上找到的信息来看,这种配置应该可以解决问题。

这是 /etc/apache2/sites-available/000-default.conf 的全部内容:

    <VirtualHost *:80>
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html

            <Directory /var/www/html>
                    Order allow,deny
                    Deny from all
                    Allow from my.ip.add.res
            </Directory>

            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>

但是,当我测试它时,我从所有地方都收到 403 错误,包括允许的“my.ip.add.res”IP 地址。

我花了很多时间在谷歌上搜索,但据我所知,这应该可以完美运行。不知道为什么不行。我是不是忽略了某些显而易见的东西?

答案1

如果您使用的是 Apache 2.4,请确保加载authz_core模块

删除:

Order allow,deny
Deny from all
Allow from my.ip.add.res

并取代已删除的指令,

插入:

Require ip xxx.xxx.xxx.xxx

如果您使用的是 Apache 2.2,请确保加载authz_host模块

删除:

Order allow,deny
Deny from all
Allow from my.ip.add.res

并取代已删除的指令,

插入:

Order Deny,Allow
Deny from all
Allow from xxx.xxx.xxx.xxx

答案2

Colt 的答案是正确的。我想分享一个来自 Apache 2.4 的运行示例,virtualhost.conf

以下是阻止访问 /admin URL 的指令(不是目录) 仅到我的网络上的内部 IP。

<Location /admin>
    Require ip 192.168.1.0/24
</Location>

答案3

我想使用特定的 IP 地址访问 PHPMyAdmin,我尝试了以下方法,但没有成功

Order Deny,Allow
Deny from all
Allow from xxx.xxx.xxx.xxx

尝试了下面的方法,它对我有用,首先,检查 apache 模块修改 mod_access_compat应该存在,并在虚拟主机中添加以下内容。

    Alias /phpmyadmin /var/www/phpmyadmin
    <Directory /var/www/phpmyadmin>
     Require ip xx.xxxx.xx.xx xx.xx.xx.xx
    </Directory>

相关内容