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

时间:2016-05-15 07:51:26

标签: apache subdomain virtualhost document-root

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

## 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}的文档根目录 - 这样做的原因是因为我主要使用Laravel开发,并且做得很好使用subdomain routing

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

使用上述VirtualHost配置,请求 test.example.local 将在D:\var\www\example\public_html处提供文档根目录。

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

2 个答案:

答案 0 :(得分:0)

迈克,早上好,

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

## 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>

这将所有子域请求路由到&#34; D:\ var \ www \ example \ test \ public_html&#34;和所有其他人到&#34; D:\ var \ www \ example \ public_html&#34;

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

答案 1 :(得分:0)

是的,所以我认为我不应该让事情变得复杂(elephant/mosquito metaphor),但仍然应该遵守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上下文中的mod_rewrite approach要好,因为此方法可以正确设置DOCUMENT_ROOT

在一天结束的时候,试图让Apache为我自动化一切,从而弯曲到我非常具体的意志,不是要走的路 - 所以一点点手动覆盖就可以了,同时保持一切干燥。

这也可以通过&#c; macrofying&#39;进一步推进。 <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>