启用 cloudflare 后虚拟主机不工作

virtual host not working when cloudflare is enabled

在使用 cloudflare 之前,我可以访问 phpMyAdmin link 例如

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 ^(.*)$  [R=200,L]
</VirtualHost>

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

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

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

当有一个选项拒绝时,我相信你需要使用 RequireAll 而不是 RequireAny(因为 RequireAny 是 2.4 中的默认行为,所以不需要像你那样指定它)。试试吧。

已编辑 -- 见下文;

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

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

他们还提供了一种简单的 "text/plain" 格式,遵循两个论文 urls:

CloudFlare IPv4 Ranges & CloudFlare IPv6 Ranges

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

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

EDIT -- 自此 post 之后我创建了一个新版本。其实今天(2017.05.31)。 url 仍然有效。它有一个 --help 函数,详细说明如何设置为 cronjob 并包含到 nginx 中。使用相同的 CloudFlare urls。