首页 > 解决方案 > 有什么方法可以简化对 apache httpd.conf 文件中特定源 IP 的访问限制

问题描述

例如,这是我的 httpd.conf:

...
<Location /a>
    Order deny,allow
    Deny from all
    Allow from x.x.x.x
    (and other "Allow from x.x.x.x")
</Location>

<Location /b>
    Order deny,allow
    Deny from all
    Allow from x.x.x.x
    (and other "Allow from x.x.x.x")
</Location>

(and other locations with same ip restrict)
...

标签里面Location都是相同的ip限制,有上百个ip。那么如何简化呢?

标签: apache

解决方案


您可以将其放在文档根目录的“”块中,而不是将“允许来自”放在每个位置内。另一种选择可能是使用重写规则并将其放置在虚拟主机级别(或根据您的配置在 httpd.conf 中):

RewriteCond %{REMOTE_ADDR} ^214.53.25.(6[4-9]|7[0-9]|8[0-9]|9[0-9])$ [OR]
RewriteCond %{REMOTE_ADDR} ^214.53.25.1([0-1][0-9]|2[0-8])$
RewriteRule .* - [F]

推荐阅读