【问题标题】:virtual host not working when cloudflare is enabled启用 cloudflare 时虚拟主机不工作
【发布时间】:2017-02-20 21:02:21
【问题描述】:

在 cloudflare 之前,我可以访问 phpMyAdmin 链接,例如

example.com/ctrl/pmasetup

在 CloudFlare 之后,我得到一个 403(来自我自己的 apache 服务器,而不是 CloudFlare 的错误页面)

Forbidden

You don't have permission to access /ctrl/pmasetup on this server.

我觉得我需要在服务器的某处添加 CloudFlare 的反向代理,我在 /etc/hosts 中做过,现在看起来像:

127.0.0.1 localhost localhost.localdomain
104.25.68.32 example.com <- This is the ip of my site when CloudFlare is enabled on it

我还在虚拟主机配置中的httpd.conf 中添加了它:

<VirtualHost *:80>
        ServerAdmin support@example.com
        DocumentRoot /var/www/example.com
        ServerName example.com
        ErrorLog logs/example.com-error_log
        CustomLog logs/example.com-access_log combinedio
        Alias /ctrl /var/www/ctrl
        <Location /ctrl>
                <RequireAny>
                        Require all denied
                        Require ip {my ip}
                        Require ip 104.25.68.32 <- CloudFlare IP, again
                </RequireAny>
        </Location>
        #Header always set Access-Control-Allow-Origin "http://example.com"
        Header always set Access-Control-Allow-Origin "*"
        Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS"
        Header always set Access-Control-Max-Age "1000"
        Header always set Access-Control-Allow-Headers "*"

        RewriteEngine On
        RewriteCond %{REQUEST_METHOD} OPTIONS
        RewriteRule ^(.*)$ $1 [R=200,L]
</VirtualHost>

编辑:即使在评论整个 RequireAny 块时,启用 CloudFlare 时我仍然会收到 403。

编辑 2:如果在 location 标签内我只有 Require all granted,那么它可以工作。那么,除了我最初放置的几个 IP,我怎么能禁止所有人呢?

编辑 3:这很可能是因为 CloudFlare 将您的 IP 更改为访问者,然后我的 apache 配置无法识别它。有什么想法吗?

【问题讨论】:

  • 安装mod_cloudflare,保持旧的工作规则。如果他们将范围添加到 ips,请务必更新您的配置
  • ... 如果您无法安装 mod_cloudflare,请改为检查 CF-Connecting-IP 标头,但查看 REMOTE_ADDR 是否在范围内ip 页面,因此请准备好创建大量规则(您可以为禁止的RewriteRule 设置大量的RewriteCond

标签: apache .htaccess virtualhost cloudflare


【解决方案1】:

已编辑 -- 见下文;

我认为您应该允许 cloudflare 的 IP 范围访问该站点。毕竟,请求来自 cloudflare 而不是客户端。

CloudFlare 的 ip 范围可以在他们的网站上找到: https://www.cloudflare.com/ips/

它们还提供了一个简单的“文本/纯文本”格式,遵循这两个网址:

CloudFlare IPv4 Ranges & CloudFlare IPv6 Ranges

还有一个我知道的用于 nginx 的小工具,位于:

https://www.8ball.me/nginx/ngx-cfips.zip

编辑 -- 自这篇文章以来我创建了一个新版本。今天实际上(2017.05.31)。网址仍然有效。它有一个 --help 函数,详细说明了如何设置为 cronjob 并包含到 nginx 中。使用相同的 CloudFlare url。

【讨论】:

    【解决方案2】:

    当有一个选项拒绝时,我认为您需要使用 RequireAll 而不是 RequireAny(因为 RequireAny 是 2.4 中的默认行为,因此无需像您一样指定它)。试试看。

    【讨论】:

    • 我将RequireAny 更改为RequiredAll -- 同样的问题。
    猜你喜欢
    • 2014-07-04
    • 2013-04-10
    • 2017-07-05
    • 2016-11-07
    • 2018-05-09
    • 2011-03-08
    • 2016-11-27
    相关资源
    最近更新 更多