本文介绍了Apache的多VirtualDocumentRoot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux系统上使用Apache2的是有办法有使用多个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的虚拟主机我第一节有:

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通常会选择第一个虚拟主机的服务器名称 ServerAlias​​ 匹配在主机 HTTP头中提供的主机名。在你的情况,因为你没有服务器名称指令,阿帕奇据说使用的IP地址伪造的服务器名称的反向DNS查找,和presuming,反向DNS导致domain.com的,不匹配,那么阿帕奇默认为第一个虚拟主机。听起来很复杂,我知道... ...的底线是,你应该使用服务器名称 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>

这应该使用 /var/www/domain.com http://domain.com 和 /var/www/www.domain.com :// WWW。 domain.com ,这两者都是由第一虚拟主机服务,而 /var/www/sub.domain.com 为, /var/www/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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 12:07