【问题标题】:Apache VirtualHosts: two subdomain configurations possible?Apache VirtualHosts:可能有两种子域配置?
【发布时间】:2016-09-11 04:08:47
【问题描述】:

目前,我在我的 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}s 文档根目录——这样做的原因是因为我主要使用Laravel 进行开发,并充分利用了subdomain routing

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

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

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

【问题讨论】:

  • 您为此定义了单独的虚拟主机。这允许使用文字路径规范。但是请注意,使用单独的主机名(有时称为“子域”)可能会导致浏览器在使用 https 时应用的安全预防措施出现问题(当然应该这样做)。
  • 是的,我想可能是这样。那么问题就变成了一个关于冲突的问题——Apache 将如何确定使用哪个虚拟主机?我先定义哪个?
  • 或者你是说我需要为每个人专门定义一个主机?
  • 如文档所述,第一个定义的虚拟主机用作后备。除此之外,顺序并不重要。匹配的主机由 http 请求中的 HOST 标头选择。这就是基于名称的虚拟主机的工作方式。注意:“基于名称的虚拟主机”,而不是基于地址的虚拟主机:httpd.apache.org/docs/current/vhosts/examples.html
  • 是的,您为每个主机名定义了一个单独的虚拟主机。您可以为那些与所有定义相同的定义包含一个共享配置文件。但是主机定义本身应该是分开的。

标签: apache subdomain virtualhost document-root


【解决方案1】:

早上好,迈克,

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

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

【讨论】:

  • 谢谢,但没有。我的问题很具体,因为我还需要能够拥有指向主域文档根目录的子域。
【解决方案2】:

对,所以我认为我不应该让事情变得复杂(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 为我自动化一切,从而屈从于我非常具体的意愿,这不是要走的路 - 所以一点点手动覆盖就可以了,同时保持一切干燥。

这也可以通过“宏化”&lt;Directory&gt; 更进一步:

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

【讨论】:

    猜你喜欢
    • 2019-11-11
    • 2016-08-01
    • 2023-03-24
    • 2014-09-06
    • 1970-01-01
    • 2013-07-08
    • 2014-06-04
    • 2014-05-30
    • 2018-02-14
    相关资源
    最近更新 更多