Apache 多个 VirtualDocumentRoot [英] Apache Multiple VirtualDocumentRoot
问题描述
在 Linux 系统上使用 Apache2 有没有办法使用 mod_vhost_alias 来拥有多个 VirtualDocumentRoot?
Using Apache2 on a Linux system is there a way to have multiple VirtualDocumentRoot using mod_vhost_alias?
这是我目前正在使用并希望继续使用的命名约定:
This is naming convention I am currently using and would like to continue to use:
host directory
127.0.0.1 domain domain.com
127.0.0.1 sub.domain domain.com_sub
然后在我的 httpd.conf 的 vhosts 部分中:
Then in my vhosts section of the httpd.conf I have:
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
VirtualDocumentRoot /var/www/%0.0.com
</VirtualHost>
<VirtualHost 127.0.0.1>
VirtualDocumentRoot /var/www/%2.0.com_%1
</VirtualHost>
这个问题是当我访问 sub.domain 时,Apache 错误日志显示它正在寻找/var/www/sub.domain.com 而不是/var/www/domain.com_test 这让我相信它只读取第一条规则然后失败,但我希望它做的是使用满足两个 VirtualDocumentRoot 规则之一的任何文档根.
The problem with this is when I visit sub.domain the Apache error log shows that it is looking for /var/www/sub.domain.com rather than /var/www/domain.com_test which leads me to believe it only reads the first rule and then fails, but what I would like it to do is use any document root that satisfies either of the two VirtualDocumentRoot rules.
推荐答案
Apache 通常会选择 ServerName
或 ServerAlias
与Host
HTTP 标头.在您的情况下,由于您没有 ServerName
指令,因此 Apache 应该在 IP 地址上使用反向 DNS 查找来伪造服务器名称,并假设反向 DNS 指向 domain.com,这不会t 匹配,Apache 则默认为第一个虚拟主机.听起来很复杂,我知道...最重要的是,您应该使用 ServerName
和 ServerAlias
使配置显式.尝试更像这样的事情:
Apache typically will pick the first virtual host whose ServerName
or ServerAlias
matches the host name provided in the Host
HTTP header. In your case, since you have no ServerName
directives, Apache supposedly uses a reverse DNS lookup on the IP address to fake a server name, and presuming that the reverse DNS leads to domain.com, which doesn't match, Apache then defaults to the first virtual host. Sounds complicated, I know... the bottom line is, you should use ServerName
and ServerAlias
to make the configuration explicit. Try something more like this:
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
ServerName domain.com
ServerAlias www.domain.com
VirtualDocumentRoot /var/www/%0
</VirtualHost>
<VirtualHost 127.0.0.1>
ServerName sub.domain.com
ServerAlias *.domain.com
VirtualDocumentRoot /var/www/%2.%3_%1
</VirtualHost>
对于 http://domain.com 应该使用 /var/www/domain.com
a> 和 /var/www/www.domain.com
用于 http://www.domain.com,两者都由第一个 vhost 提供,而 /var/www/sub.domain.com
用于 http://sub.domain.com, /var/www/blah.domain.com
用于 http://blah.domain.com 等.
That should use /var/www/domain.com
for http://domain.com and /var/www/www.domain.com
for http://www.domain.com, both of which are served by the first vhost, and /var/www/sub.domain.com
for http://sub.domain.com, /var/www/blah.domain.com
for http://blah.domain.com, and so on.
这篇关于Apache 多个 VirtualDocumentRoot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!