Apache VirtualHosts:两个子域配置可能吗?

Apache VirtualHosts: two subdomain configurations possible?

目前,我在我的 Apache 2.4 服务器 (运行 EasyPHP) 上设置了以下 VirtualHost 配置:

## Local domain access
<VirtualHost 127.0.0.1>
    UseCanonicalName Off
    ServerAlias *.local *.*.local
    VirtualDocumentRoot "D:/var/www/%-2/public_html"
    <Directory "D:/var/www/*/public_html">
        Require all granted
        Options Includes Indexes FollowSymLinks
        AllowOverride All
    </Directory>
</VirtualHost>

本质上,这将 {domain}.local{sub}.{domain}.local 映射到 {domain} 的文档根目录 - 这样做的原因是我主要使用 [=31 进行开发=],善用subdomain routing.

但是,在某些情况下,我需要让子域请求指向域的文档根父目录中的另一个目录,从而调用具有自己的文档根的不同应用程序。

使用上述 VirtualHost 配置,请求 test.example.local 将在 D:\var\www\example\public_html.

提供文档根目录

我可以对此配置进行哪些添加或更改以允许上述示例服务于 D:\var\www\example\test\public_html,但前提是 test 目录存在?

早上好迈克,

我建议创建单独的虚拟主机

## Local domain access
<VirtualHost 127.0.0.1>
    UseCanonicalName Off
    ServerAlias *.*.local
    VirtualDocumentRoot "D:\var\www\example\test\public_html"
    <Directory "D:\var\www\example\test\public_html">
        Require all granted
        Options Includes Indexes FollowSymLinks
        AllowOverride All
    </Directory>
</VirtualHost>

## Local domain access
<VirtualHost 127.0.0.1>
    UseCanonicalName Off
    ServerAlias *.local
    VirtualDocumentRoot "D:\var\www\example\public_html"
    <Directory "D:\var\www\example\public_html">
        Require all granted
        Options Includes Indexes FollowSymLinks
        AllowOverride All
    </Directory>
</VirtualHost>

这会将所有子域请求路由到 "D:\var\www\example\test\public_html",将所有其他请求路由到 "D:\var\www\example\public_html"

可在此处找到文档: https://httpd.apache.org/docs/current/vhosts/examples.html

是的,所以我认为我不应该让事情变得复杂(), but should still comply with the DRY principal 这样我的虚拟主机配置文件中就不会出现大量异常 - 总而言之,这变得非常累人,而且难以维护。

因此,我选择使用 Apache 的 mod_macro 模块 - 这需要明确启用。

结果是这样的:

# Macro LocalSub
# For specific *.*.local subdomains that require their own DIRECTORY_ROOT
<Macro LocalSub $sub $domain>
    <VirtualHost 127.0.0.1>
        ServerName $sub.$domain.local
        DocumentRoot "D:/var/www/$domain/$sub/public_html"
        <Directory "D:/var/www/$domain/$sub/public_html">
            Require all granted
            Options Includes Indexes FollowSymLinks
            AllowOverride All
        </Directory>
    </VirtualHost>
</Macro>

# Specific subdomain Macro usage
Use LocalSub assets example
Use LocalSub images01 mydomain
Use LocalSub images02 mydomain

# Fallback to *.local and *.*.local (points to the same DOCUMENT_ROOT)
<VirtualHost 127.0.0.1>
    UseCanonicalName Off
    ServerAlias *.local *.*.local
    VirtualDocumentRoot "D:/var/www/%-2/public_html"
    <Directory "D:/var/www/*/public_html">
        Require all granted
        Options Includes Indexes FollowSymLinks
        AllowOverride All
    </Directory>
</VirtualHost>

这比 VirtualHost 上下文中的 更好,因为此方法允许正确设置 DOCUMENT_ROOT

归根结底,试图让 Apache 为我自动化一切,从而屈服于我非常具体的意志,并不是可行的方法 - 所以一点点手动覆盖就可以解决问题,同时保留一切干

还可以通过 'macrofying' <Directory>:

更进一步
# Macro Directory
# Default Directory configuration on a per-vhost basis
<Macro Directory $dir>
    <Directory "D:/var/www/$dir/public_html">
        Require all granted
        Options Includes Indexes FollowSymLinks
        AllowOverride All
    </Directory>
</Macro>

# Macro LocalSub
# For specific *.*.local subdomains that require their own DIRECTORY_ROOT
<Macro LocalSub $sub $domain>
    <VirtualHost 127.0.0.1>
        ServerName $sub.$domain.local
        DocumentRoot "D:/var/www/$domain/$sub/public_html"
        Use Directory $domain/$sub
    </VirtualHost>
</Macro>

# Specific subdomain Macro usage
Use LocalSub assets example
Use LocalSub images01 mydomain
Use LocalSub images02 mydomain

# Fallback to *.local and *.*.local (points to the same DOCUMENT_ROOT)
<VirtualHost 127.0.0.1>
    UseCanonicalName Off
    ServerAlias *.local *.*.local
    VirtualDocumentRoot "D:/var/www/%-2/public_html"
    Use Directory *
</VirtualHost>