【问题标题】:Virtualhost wildcard subdomain of wildcard subdomain in apache2.4apache2.4中通配符子域的virtualhost通配符子域
【发布时间】:2020-08-23 08:57:19
【问题描述】:

目前,我进行了如下通配符子域配置:

<VirtualHost *:80>
    ServerAlias *.domain
    ErrorLog /tmp/error.log
    CustomLog /tmp/access.log combined
    VirtualDocumentRoot /var/www/%1/public
<Directory "/var/www">
    DirectoryIndex index.php
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
</VirtualHost>

但如果我输入 sub.sub.domain 将返回到默认虚拟主机。如何配置到主/根域的路径?我在conf下试过了,但还是不行:

<VirtualHost *:80>
    ServerAlias *.*.domain
    ErrorLog /tmp/error.log
    CustomLog /tmp/access.log combined
    VirtualDocumentRoot /var/www/%2/public
<Directory "/var/www">
    DirectoryIndex index.php
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
</VirtualHost>

我把 %2 改成 %1 还是不行。

什么是正确的语法?

【问题讨论】:

    标签: apache apache2 subdomain virtualhost vhosts


    【解决方案1】:

    上面的代码有正确的语法,但我只是把它放在错误的放置顺序上。所以正确的答案是把第二个放在第一个上面。

    下面的完整配置示例:

    <VirtualHost *:80>
        ServerAlias *.*.domain
        ErrorLog /tmp/error.log
        CustomLog /tmp/access.log combined
        VirtualDocumentRoot /var/www/%2/public
    <Directory "/var/www">
        DirectoryIndex index.php
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerAlias *.domain
        ErrorLog /tmp/error.log
        CustomLog /tmp/access.log combined
        VirtualDocumentRoot /var/www/%1/public
    <Directory "/var/www">
        DirectoryIndex index.php
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    </VirtualHost>
    

    Apache 按从上到下的升序跳过/忽略相同的子域/域配置。 sub.sub.domain 也是 *.domain 的一部分,所以如果我想配置它,我必须将配置放在顶部,或者如果使用不同的文件配置,请使用较小的数字名称。

    多个 ServerAlias 或 ServerName 或 NameVirtualHostlines 将产生“NameVirtualHost *:80 has no VirtualHosts”警告。不过,Apache 将忽略第二个指令并使用第一个定义的 NameVirtualHost 行。当一个人使用多个虚拟主机配置文件并且不了解您只需要定义一个特定的 NameVirtualHost 行时,这似乎会发生

    reference

    简单的解决方案,但我解决了几个小时,我希望它可以帮助像我这样的其他人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-12
      • 2012-11-14
      • 2010-09-09
      • 2011-07-28
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      相关资源
      最近更新 更多