Cloudflare 和 nginx:重定向太多

Cloudflare and nginx: Too many redirects

我正在尝试设置 NGINX 和 cloudflare。 我在 Google 上看到过这个,但没有解决我的问题。我的 cloudflare 目前处于活动状态。我删除了 cloudflare 中的所有页面规则,但之前有 domain.com 和 www.domain.com 使用 HTTPS。我认为这可能是导致问题的原因,所以我删除了它。这是我的 default NGINX 文件,目的是只允许通过域名访问,禁止通过网站的 IP 值访问:

server{

  #REDIRECT HTTP TO HTTPS

  listen 80 default;
  listen [::]:80 default ipv6only=on; ## listen for ipv6
  rewrite ^ https://$host$request_uri? permanent;

}

server{

  #REDIRECT IP HTTPS TO DOMAIN HTTPS       

    listen 443;
    server_name numeric_ip;
    rewrite ^ https://www.domain.com; 

}

server{

  #REDIRECT IP HTTP TO DOMAIN HTTPS

    listen 80;
    server_name numeric_ip;
    rewrite ^ https://www.domain.com;

}

server {

         listen 443 ssl;
         server_name www.domain.com domain.com;
         #rewrite ^ https://$host$request_uri? permanent;
         keepalive_timeout 70;

         ssl_certificate     /ssl/is/working.crt;
         ssl_certificate_key /ssl/is/working.key;

         ssl_session_timeout 1d;
         ssl_session_cache shared:SSL:50m;

         #ssl_dhparam /path/to/dhparam.pem;

         ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
         ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM$
         ssl_prefer_server_ciphers on;

         add_header Strict-Transport-Security max-age=15768000;

         (...) more ssl configs

什么可以关闭?如果需要我会提供更多信息...

这些关于 运行-away 重定向的问题一直都在出现!

通常,问题在于 301 Moved Permanently responses are often cached within the browsers "for good", and there is often no way to CtrlR nor CtrlShiftR out of it, short of clearing the whole cache. (This is one of the reasons I often prefer 302 Found / 302 Moved Temporarily 而不是,尤其是在开发阶段,因为 302 默认情况下通常根本不缓存响应。)

此外,如果您过去有过 HSTS,并且它已成功获取并由浏览器在后台悄悄安装,并且从未明确清除或过期,那么浏览器将永远不会发出任何后续请求超过 http:// 直到且除非政策被清除 - 所有请求将始终超过 https://.

至于将 CloudFlare 加入其中,它是否首先减少了拥有这么多不同服务器定义和重定向的需要,因为您的 IP 地址应该被隐藏?我不确定将您的 IP 地址隐藏在 CloudFlare 背后有什么好处,但公开揭示它为任何进行全球互联网扫描的人提供的域名。

由于您已经 运行 通过了 CloudFlare 提供的所有 "SSL modes",我建议您将所有 301 permanent 重定向更改为 302 临时 redirect(如果不要首先完全删除所有这些),清除浏览器的缓存,然后再次尝试绕过 ssl 选项。 :-)

经过试用,我发现这只与Cloudflare有关。因为在迁移到 Cloudflare 之前我没有遇到重定向问题。

在我的例子中,这是一个像这样的简单修复。 Select [加密] 框和 select 完全(严格),如图所示。

真的,您可以先尝试这个,然后再进行任何其他操作。

转到页面规则部分并检查您是否有“始终重定向到 https”规则。我默认启用它。

@prosti 提供了解决方案。我将在此处添加一些关于为什么会发生重定向循环的解释。

在 Nginx 服务器前面设置 Cloudflare CDN 之后。客户端不再直接访问 Nginx。内容由 Cloudflare 提供的中间媒体代理获取。问题的原因是 这个代理不遵循 Nginx 上设置的重定向。或者你可以认为它是硬编码的。

不同于遵循 302/301 重定向的网络浏览器。代理的行为,通过 HTTP 或 HTTPS 访问我们 VPS 上的 Nginx,在 Cloudflare Dashboard -> "SSL/TLS".

中配置

解决方法是配置高于“Full”的加密级别

Cloudflare 的支持团队给出了 cause and solution。清晰且有帮助。

Troubleshooting redirect loop errors

Resolve redirect loop (too many redirects) errors that prevent visitors from viewing your website.

Cloudflare SSL options incompatible with your origin web server

The most common cause of redirect loops is due to a combination of

  • a redirect performed by your origin web server, and
  • a Cloudflare SSL option that is incompatible with the redirect performed by your origin.

Cause

The “Flexible” SSL encryption mode in the Cloudflare “SSL/TLS” app, “Overview” tab, encrypts traffic between the browser and the Cloudflare network over HTTPS. However, when the “Flexible” SSL option is enabled, Cloudflare sends requests to your origin web server unencrypted over HTTP. Redirect loops occur if your origin web server is configured to redirect all HTTP requests to HTTPS when using the “Flexible” SSL option.

Redirect loops may also occur when using the “Full” or “Full(strict)” SSL option. The only difference is that Cloudflare contacts your origin web server over HTTPS and the redirect loop occurs if your origin web server redirects HTTPS requests to HTTP.

Resolution

Update the Cloudflare SSL option in the “SSL/TLS” app, “Overview” tab.

  • If currently set to “Flexible”, update it to “Full” if you have an SSL certificate configured at your origin web server.
  • If currently set to “Full”, try updating it to “Flexible.”

Cloudflare 将 Cdn-Loop: cloudflare header 发送到原始服务器。此 Cdn-Loop 作为标准提交。 参见:https://datatracker.ietf.org/doc/html/rfc8586

这适用于 nginx。仅在未访问时重定向到 https by/from CDN:

server {
     # ..

     if ($http_cdn_loop ~ "^$") {
         return 301 https://$host$request_uri;
     }
}

也可以使用$http_cf_visitor:

server {
        # ..

        if ($http_cf_visitor ~ '{"scheme":"http"}') {
            return 301 https://$host$request_uri;
        }
}

另见“如果是邪恶的”:

https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/