关于 http 重定向 https 及启用 https 产生一些问题的探索
By 辞峡烟斜
最近,在利用 acme.sh 测试域名 hfvr.limz.win 启用 https 的过程当中(参看https://www.yiwan.org/index.php/ltd_documents/363-using-acmesh-to-start-https.html),发现了一些问题,浏览器一开始访问正常,后来(也不知道过了多久)就出现 “ERR_TOO_MANY_REDIRECT” 错误,网页无法正常运作,如下图所示:
<a href=“https://pich.yiwan.org/YiWan/201905/20190515X01H20190511170038.jpg" data-fancybox=“images” class=“cboxElement” rel=“example_group"417>
但是有时候,又可以正常访问:
<a href=“https://pich.yiwan.org/YiWan/201905/20190515X02H20190511170304.jpg" data-fancybox=“images” class=“cboxElement” rel=“example_group"417>
反正不定,换个浏览器说不准又能访问,但百分之八九十的几率不能访问。根据错误提示,清空 Cookie 也是不能解决。
问题甚为蹊跷,因为之前在域名 www.yiwan.org (即本站)启用 https 时一切比较顺利,www.yiwan.org 也一直正常,未出现任何问题,所以是直接拷贝 www.yiwan.org 网站 Apache2 配置文件来用,稍微更改下就在其他 VPS 上用于 hfvr.limz.win 测试,同样的环境,同样的配置,同样的DNS服务商(Cloudflare),www.yiwan.org 访问没问题,到了 hfvr.limz.win 上就出现问题了。
网站配置文件:
<a href=“https://pich.yiwan.org/YiWan/201905/20190515X00H2019051595153.jpg" data-fancybox=“images” class=“cboxElement” rel=“example_group"417>
于是四处求索,首先怀疑问题出在 letsencrypt 新版本上,网上还真有人询问,比较典型且助于问题解决的是以下三篇:
1.https://community.letsencrypt.org/t/always-redirect-to-https/10838/8
2.https://community.letsencrypt.org/t/apache-with-lets-encrypt-non-www-https-only-the-page-isn-t-redirecting-properly/43613
3.https://github.com/certbot/certbot/issues/4198
尤其是最后一篇算是暂时解决了问题:
<a href=“https://pich.yiwan.org/YiWan/201905/20190515X03H20190511163300.jpg" data-fancybox=“images” class=“cboxElement” rel=“example_group"417>
<a href=“https://pich.yiwan.org/YiWan/201905/20190515X04H20190511163032.jpg" data-fancybox=“images” class=“cboxElement” rel=“example_group"417>
那就是直接按红圈中建议,直接删除80端口中的重定向语句。后经测试,注释掉或者将 RewriteEngine 设为 off 也是可以暂时解决问题的。
可这样,总是感觉治标不治本,心里在想一万个为什么。为什么同样的环境和配置,到了 hfvr.limz.win 就不行了呢。
后来的后来,在不小心看 Cloudflare 的设置中,感觉到了可能是 Cloudflare 的问题。
在 Cloudflare 的“Crypto”页有有关 SSL 的设置信息:
<a href=“https://pich.yiwan.org/YiWan/201905/20190515X07H20190514160042.jpg" data-fancybox=“images” class=“cboxElement” rel=“example_group"417>
<a href=“https://pich.yiwan.org/YiWan/201905/20190515X06H671305909.png" data-fancybox=“images” class=“cboxElement” rel=“example_group"417>
<a href=“https://pich.yiwan.org/YiWan/201905/20190515X11H20190514170703.jpg" data-fancybox=“images” class=“cboxElement” rel=“example_group"417>
<a href=“https://pich.yiwan.org/YiWan/201905/20190515X12H20190514170726.jpg" data-fancybox=“images” class=“cboxElement” rel=“example_group"417>
<a href=“https://pich.yiwan.org/YiWan/201905/20190515X13H740933459.png" data-fancybox=“images” class=“cboxElement” rel=“example_group"417>
并为此专门配置测试了下。
<a href=“https://pich.yiwan.org/YiWan/201905/20190515X05H20190514163709.jpg" data-fancybox=“images” class=“cboxElement” rel=“example_group"417>
当 “Crypto”中 SSL 设置为 Off 时,网站是打不开的,当设置为 Flexible 时,打开的是80端口的网站页:
<a href=“https://pich.yiwan.org/YiWan/201905/20190515X08H20190514161939.jpg" data-fancybox=“images” class=“cboxElement” rel=“example_group"417>
当设置为 Full 或者 Full(strict) 时,打开的是443端口的网站页:
<a href=“https://pich.yiwan.org/YiWan/201905/20190515X10H20190514155818.jpg" data-fancybox=“images” class=“cboxElement” rel=“example_group"417>
有关Cloudflare SSL证书开通和设置使用,请浏览以下两篇文章了解详情:
1.https://linkthis.me/2018/02/16/the-note-of-using-cloudflare/
2.https://www.daniao.org/2387.html
篇首出现的问题可能就是在 Cloudflare 的“Crypto”页设置 SSL 的原因,关闭相关功能就OK,但是现象无法复现,所以也无法真正确定是 Cloudflare 的问题,本文权当为此问题解决提供一个参考意见。
坐定不经想,新网站为什么要开放80端口,直接443 SSL,就不会有这么多幺蛾子了,就这么实施。