V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
nowheretoseek
V2EX  ›  问与答

<VirtualHost *:80>为什么总拦截处理 https 的请求?

  •  
  •   nowheretoseek · 2022-04-17 15:23:59 +08:00 · 777 次点击
    这是一个创建于 751 天前的主题,其中的信息可能已经有所发展或是发生改变。

    如下一份配置,本来是将 http 重定向 https 的,之前用在 club 域名上,一切正常,现在换成 com 域名,结果总导致过多重定向错误。(另一份处理 443 端口的没帖上来。)

    确定是这个配置的问题,是因为将 RewriteEngine on 改成 off 后,问题就解决了。可是这个虚拟主机是处理 80 端口的问题的啊。为什么访问 https://www.example.com 的时候,他总要插手呢?

    <VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
    
        DocumentRoot /var/www/html/example
        <Directory /var/www/html/example/>
                Options -Indexes +FollowSymLinks
                Include /etc/apache2/custom.d/globalblacklist.conf
                DirectoryIndex index.php
                AllowOverride All
                Require all denied
        </Directory>
    
        ErrorLog ${APACHE_LOG_DIR}/example_error.log
        CustomLog ${APACHE_LOG_DIR}/example_access.log combined
    
        RewriteEngine on
        RewriteCond %{SERVER_NAME} =example.com
        RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
        RewriteCond %{SERVER_NAME} =www.example.com
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    </VirtualHost>
    
    
    8 条回复    2022-04-19 18:22:21 +08:00
    nowheretoseek
        1
    nowheretoseek  
    OP
       2022-04-17 17:00:14 +08:00
    问题已解决,是 cloudflare 的问题,开启了 Development Mode 可是还是用了缓存。
    将 dns 中的 a 记录从 proxied 改成 dns only ,或者开着 Development Mode 时清除 cache 问题消失了,不知何故。
    是我对 Development Mode 理解有误,还是怎么了?

    Development Mode 的备注解释是:
    Temporarily bypass our cache. See changes to your origin server in realtime.
    Showfom
        2
    Showfom  
       2022-04-17 17:07:05 +08:00
    CF 的话,你打开 SSL/TLS 设置,改成 Full 或者 Full (strict)

    然后 Edge Certificates 里 Always Use HTTPS 打开
    nowheretoseek
        3
    nowheretoseek  
    OP
       2022-04-18 01:37:27 +08:00
    @Showfom 谢谢,问题解决了。请教一下:
    我的 virtualhost 配置文件没问题,dns only 时,可以实现 example.com 重定向到 www.example.com ,http 重定向到 https

    为什么用了 proxied 就会过多重定向呢?而做了你说的两项设置后,问题就解决了。

    SSL/TLS 默认是 flexible ,Edge Certificates 没打开 Always Use HTTPS ,还是不太清楚其中的原理,为什么改了这两个配置项就没有循环重定向了?
    Showfom
        4
    Showfom  
       2022-04-18 12:10:45 +08:00   ❤️ 1
    @nowheretoseek #3 因为你不打开的话,CF 会默认请求 80 端口,造成无限循环了
    nowheretoseek
        5
    nowheretoseek  
    OP
       2022-04-18 15:45:34 +08:00
    @Showfom 原来如此,感谢指教,真是个大坑啊……
    Showfom
        6
    Showfom  
       2022-04-18 15:57:44 +08:00   ❤️ 1
    @nowheretoseek #5

    因为不是所有人都要 https 的,好多还用 http 的,他默认的逻辑是

    Flexible 模式下

    80 端口反代 80 端口
    443 端口也反代 80 端口

    但是你本地设置了 80 端口按照请求的 server_name 跳转 443 ,他就无限跳转了

    SSL/TLS 设置 Full 以后,443 端口只会去请求你服务器的 443 端口,不会再去请求 80 端口,当然为了防止 CF 中间人攻击,建议使用 Full (strict),你本地的 443 端口需要一张可信的证书

    如果 80 端口不需要特殊的设置,只想要他跳转 443 的话,开启 Always Use HTTPS 他就会自动跳转并且不会去请求你服务器的 80 端口。
    nowheretoseek
        7
    nowheretoseek  
    OP
       2022-04-18 22:50:04 +08:00
    @Showfom 谢谢,这样就说得通了,如果我早些仔细看 Flexible 的说明,就不至于瞎忙活了。

    这样一来,想实现我最初目标,只要开启 Always Use HTTPS 并配置好本机的 443 虚拟主机就好了,80 端口直接可以关闭了。
    Showfom
        8
    Showfom  
       2022-04-19 18:22:21 +08:00
    @nowheretoseek #7 建议都打开,就丢给 CF 自己处理 80 端口去
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4521 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 04:09 · PVG 12:09 · LAX 21:09 · JFK 00:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.