使用cloudflare的灵活sslredirect循环

我正在尝试将CloudFlare提供的Flexible SSL实现到我的站点。

这是我的nginxconfiguration:

# PHP-FPM upstream; change it accordingly to your local config! upstream php-fpm { server 127.0.0.1:9000; } server { listen 80; server_name example.com www.example.com; return 301 https://$server_name$request_uri; } server { ## Listen ports listen 443; # use _ if you want to accept everything, or replace _ with domain server_name example.com www.example.com; location / { #proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X_FORWARDED_PROTO https; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Url-Scheme $scheme; proxy_redirect off; proxy_max_temp_file_size 0; } (...rest of config...) 

但是,当我尝试连接到网站(Wordpress),我得到一个redirect循环(铬:ERR_TOO_MANY_REDIRECTS)。 如何configurationnginx来防止这种情况?

    Cloudflare灵活的ssl意味着cloudflare和你的服务器之间的连接总是在http:

    连接总是http

    鉴于此 – 相关性问题中唯一的服务器块是这样的:

     server { listen 80; server_name example.com www.example.com; return 301 https://$server_name$request_uri; } 

    这应该是显而易见的,为什么会导致redirect循环,有2个解决scheme强制https连接使用其灵活的SSL解决scheme。

    使用Cloudflare的页面规则

    如果仅通过cloudflare访问服务器,则可以使用cloudflare自己的页面规则来修改域,子域或任何url模式的响应:

    使用cloudflare的页面规则

    其中一个选项是强制https:

    始终使用HTTPS

    testing$http_x_forwarded_proto

    有时您可能想要避免使用页面规则(应该只是less见的或过渡性的),对于这些场景,可以testing转发的协议并根据以下内容redirect:

     server { listen 80; server_name example.com www.example.com; if ($http_x_forwarded_proto = "http") { return 301 https://$server_name$request_uri; } ... directives to generate a response } 

    这可以解决您的问题,如果你有你的有效的SSL证书。 [encryption]框中,并selectFull (strict)在图像中。 在这里输入图像描述

    真的不需要更新Nginx的Web服务器configuration文件。