首页 > 解决方案 > 仅允许通过 .htaccess 的 cloudflare ip 范围给出 403 错误

问题描述

我一直试图在我的服务器上只允许 cloudflares ip 范围,但我不断收到 403 错误,403 是绕过 cloudfare 的人应该看到的

我试过了

    #cloudflare
    order deny,allow
    Deny from all
    #ipv4
    allow from 173.245.48.0/20
    allow from 103.21.244.0/22
    allow from 103.22.200.0/22
    allow from 103.31.4.0/22
    allow from 141.101.64.0/18
    allow from 108.162.192.0/18
    allow from 190.93.240.0/20
    allow from 188.114.96.0/20
    allow from 197.234.240.0/22
    allow from 198.41.128.0/17
    allow from 162.158.0.0/15
    allow from 104.16.0.0/12
    allow from 172.64.0.0/13
    allow from 131.0.72.0/22
    #ipv6
    allow from 2400:cb00::/32
    allow from 2606:4700::/32
    allow from 2803:f800::/32
    allow from 2405:b500::/32
    allow from 2405:8100::/32
    allow from 2a06:98c0::/29
    allow from 2c0f:f248::/32

我试过了

    DenyAllButCloudFlare

我遇到了同样的问题,上面的行给了我 500 错误

对于我的生活,我无法弄清楚,cloudflare 支持只是将我推荐到我关注的白名单页面

我正在使用 Apache 2.4.2

编辑:更改 Apache 版本号 :)

标签: apache.htaccesscloudflare

解决方案


尝试在 Apache 中使用 Require 指令。Apache自己提到:

mod_access_compat 提供的 Allow、Deny 和 Order 指令已被弃用,并将在未来的版本中消失。您应该避免使用它们,并避免推荐使用它们的过时教程。

尝试将其更改为,并确保 mod_authz_host 已启用:

#path to your website
<Directory "path/to/public_html/or/var/www/html">
    #ipv4
    Require ip 173.245.48.0/20
    Require ip 103.21.244.0/22
    Require ip 103.22.200.0/22
    Require ip 103.31.4.0/22
    Require ip 141.101.64.0/18
    Require ip 108.162.192.0/18
    Require ip 190.93.240.0/20
    Require ip 188.114.96.0/20
    Require ip 197.234.240.0/22
    Require ip 198.41.128.0/17
    Require ip 162.158.0.0/15
    Require ip 104.16.0.0/12
    Require ip 172.64.0.0/13
    Require ip 131.0.72.0/22
    #ipv6
    Require ip 2400:cb00::/32
    Require ip 2606:4700::/32
    Require ip 2803:f800::/32
    Require ip 2405:b500::/32
    Require ip 2405:8100::/32
    Require ip 2a06:98c0::/29
    Require ip 2c0f:f248::/32
</Directory>

查看https://httpd.apache.org/docs/2.4/howto/access.html了解更多信息。

请注意这一点:Cloudflare 自己说:我认为无论如何使用普通的 Apache 指令应该会更好

Cloudflare 不再更新和支持 mod_cloudflare,从 Linux 操作系统的 Debian 9 * 和 *Ubuntu 18.04 LTS 版本开始。我们现在支持使用 Apache Web 服务器的客户使用 mod_remoteip。有兴趣构建 mod_cloudflare 包的客户可以从 GitHub 下载代码库。

请参阅: https: //support.cloudflare.com/hc/en-us/articles/200170916-Restoring-original-visitor-IPs-Option-1-Installing-mod-cloudflare

而且 mod_remoteip 感觉它不安全。所以,我建议你坚持使用 Require ip 指令。


推荐阅读