启用cloudflare时虚拟主机无法正常工作

时间:2017-02-20 21:02:22

标签: apache .htaccess virtualhost cloudflare

在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配置无法识别它。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

已编辑 - 见下文;

我相信你应该允许cloudflare的ip范围访问该网站。毕竟,请求来自cloudflare而不是客户端。

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

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

CloudFlare IPv4 Ranges&amp; CloudFlare IPv6 Ranges

还有一个我知道的小工具,对于nginx tho,可在以下网址找到:

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

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