【问题标题】:SSL for subdomain is allowing parent domain to redirect to subdomain子域的 SSL 允许父域重定向到子域
【发布时间】:2023-03-27 22:44:01
【问题描述】:

我已为我的子域启用 SSL,一切正常。我遇到的问题是,当您为父域(不应允许 SSL 连接)包含 https 时,它会作为父域重定向到子域。

我假设我的 virtualhosts 条目中有一些不正确的内容。

有什么想法吗?

谢谢

【问题讨论】:

  • 这些域都不是sub domains,都是顶级域。
  • https 前缀意味着,浏览器在获取任何 http 内容之前需要 ssl 握手,包括重定向为 http 标头。该域的自签名证书对合法浏览器无效。
  • 我们可以查看您的.htaccess 文件吗?
  • 如果没有虚拟主机定义,每个答案都是纯粹的猜测。

标签: apache ssl subdomain virtualhost


【解决方案1】:

你没有提供很多细节,但这里是开始。

当您指定HTTPS://<hostname> 时,TCP 消息将发送到<ip address>:443。不是<hostname>:443。您的浏览器会在发送任何内容之前进行主机名-> IP 地址转换。您的浏览器还会在(加密的)消息中添加标题 Host: <hostname>

只有在解包加密消息时,网络服务器才会获取主机标头,然后可以(可能)将其路由到不同的虚拟主机。

但在解密时,它“已经”在与 SSL 虚拟主机通信(否则,apache 无法解密消息)。因此,此时,它会尝试找出“所需的”主机名是什么(通过 Hosts 标头),然后查看您是否有一个具有该名称的 :443 虚拟主机。如果没有,它会将其交给默认的 :443 虚拟主机。

【讨论】:

    【解决方案2】:

    假设

    • 您将两个域托管在同一个 httpd 实例上
    • 443 端口只有一个虚拟主机定义

    我还假设当您说“重定向到作为父域的子域”时,您的意思是应该只出现在 HTTPS 子域(即 https://sub.example.com)的内容出现在 HTTPS父域(即 https://example.com 看起来与 https://sub.example.com 完全一样)并且没有发生真正的 HTTP 重定向

    那么

    如果你有两个这样的虚拟主机条目:

    <VirtualHost *:80>
      # using parent content
      DocumentRoot "/web/parent"
    </VirtualHost>
    
    <VirtualHost *:443>
      #using subdomain content
      DocumentRoot "/web/subdomain"
    
      # All sorts of SSL config
      ....
    </VirtualHost>
    

    这导致无论您使用什么主机名:

    • 对端口 80 的任何请求都将始终生成父内容
    • 对端口 443 的任何请求都将始终生成子域内容

    所以:

    尝试添加“NameVirtualHost *:443”(如果您还没有)和至少第三个 VirtualHost:

    NameVirtualHost *:443
    
    <VirtualHost *:80>
      # the default virtualhost for port 80
      # using parent content
      DocumentRoot "/web/parent"
    </VirtualHost>
    
    <VirtualHost *:443>
      # the default virtualhost for port 443
      # using subdomain content
      ServerName sub.example.com
      DocumentRoot "/web/subdomain"
    
      # All sorts of SSL config
      ....
    </VirtualHost>
    
    <VirtualHost *:443>
      # another virtualhost for port 443 
      # only activated for example.com like https://example.com/something
      # using parent content
      ServerName example.com
      DocumentRoot "/web/parent"
    
      # All sorts of SSL config
      ....
    </VirtualHost>
    

    评估顺序很重要,因此第一个虚拟主机成为任何不匹配任何其他虚拟主机的请求的默认值。

    第三个虚拟主机需要配置为当有人在父域上请求 HTTPS 时您期望发生的任何事情:即您想要重定向回 HTTP 版本,还是只显示不同的内容?

    httpd command 有一个 -S 标志,它将输出当前有序的虚拟主机配置,然后退出,这对于诊断在哪些端口上定义了哪些虚拟主机以及关联的名称很有用

    -S
      Show the settings as parsed from the config file (currently only shows the virtualhost settings).
    

    一些配置、版本和平台会对这个问题有所帮助。

    【讨论】:

      【解决方案3】:

      ServerAdmin webmaster@localhost

      DocumentRoot /var/www
      <Directory />
          Options FollowSymLinks
          AllowOverride None
      </Directory>
      <Directory /var/www/>
          Options Indexes FollowSymLinks MultiViews
          AllowOverride None
          Order allow,deny
          allow from all
      </Directory>
      
      ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
      <Directory "/usr/lib/cgi-bin">
          AllowOverride None
          Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
          Order allow,deny
          Allow from all
      </Directory>
      
      ErrorLog ${APACHE_LOG_DIR}/error.log
      
      # Possible values include: debug, info, notice, warn, error, crit,
      # alert, emerg.
      LogLevel warn
      
      CustomLog ${APACHE_LOG_DIR}/ssl_access.log combined
      
      #   SSL Engine Switch:
      #   Enable/Disable SSL for this virtual host.
      SSLEngine on
      
      #   A self-signed (snakeoil) certificate can be created by installing
      #   the ssl-cert package. See
      #   /usr/share/doc/apache2.2-common/README.Debian.gz for more info.
      #   If both key and certificate are stored in the same file, only the
      #   SSLCertificateFile directive is needed.
      SSLCertificateFile    /etc/ssl/certs/ssl-cert-snakeoil.pem
      SSLCertificateKeyFile /etc/ssl/private/ssl-cert-snakeoil.key
      

      【讨论】:

        猜你喜欢
        • 2013-10-13
        • 1970-01-01
        • 1970-01-01
        • 2014-01-13
        • 1970-01-01
        • 2021-04-04
        • 1970-01-01
        • 2014-09-06
        • 2016-06-11
        相关资源
        最近更新 更多