Apache 多个 VirtualDocumentRoot
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/550525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache Multiple VirtualDocumentRoot
提问by Ben
Using Apache2 on a Linux system is there a way to have multiple VirtualDocumentRoot using mod_vhost_alias?
在 Linux 系统上使用 Apache2 有没有办法使用mod_vhost_alias拥有多个 VirtualDocumentRoot ?
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
Then in my vhosts section of the httpd.conf I have:
然后在 httpd.conf 的 vhosts 部分中,我有:
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>
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.
问题是当我访问 sub.domain 时,Apache 错误日志显示它正在寻找 /var/www/sub.domain.com 而不是 /var/www/domain.com_test 这让我相信它只读取第一条规则然后失败,但我希望它做的是使用满足两个 VirtualDocumentRoot 规则之一的任何文档根。
回答by David Z
Apache typically will pick the first virtual host whose ServerNameor ServerAliasmatches the host name provided in the HostHTTP header. In your case, since you have no ServerNamedirectives, 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 ServerNameand ServerAliasto make the configuration explicit. Try something more like this:
Apache 通常会选择第一个虚拟主机,其ServerName或ServerAlias匹配HostHTTP 标头中提供的主机名。在您的情况下,由于您没有ServerName指令,Apache 应该在 IP 地址上使用反向 DNS 查找来伪造服务器名称,并假设反向 DNS 指向不匹配的 domain.com,然后 Apache 默认为第一个虚拟主机。听起来很复杂,我知道……底线是,您应该使用ServerName和ServerAlias使配置明确。尝试更像这样的事情:
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>
That should use /var/www/domain.comfor http://domain.comand /var/www/www.domain.comfor http://www.domain.com, both of which are served by the first vhost, and /var/www/sub.domain.comfor http://sub.domain.com, /var/www/blah.domain.comfor http://blah.domain.com, and so on.
这应该使用/var/www/domain.com的http://domain.com和/var/www/www.domain.com对http://www.domain.com,这两者都是由第一虚拟主机服务,并/var/www/sub.domain.com为http://sub.domain.com,/var/www/blah.domain.com对于HTTP:// blah.domain.com等等。
回答by cha0s
You have to qualify the backreferences when you want to put a '.' in the file path. So you need to have it like this:
当您想放置一个 '.' 时,您必须限定反向引用。在文件路径中。所以你需要像这样:
VirtualDocumentRoot /var/www/%2.0.%3_%1
回答by starmonkey
Regarding the OP and the issue with "/var/www/html" being set:
关于 OP 和设置“/var/www/html”的问题:
The problem I had to this was using %1 instead of %2. Here's my working example:
我遇到的问题是使用 %1 而不是 %2。这是我的工作示例:
ServerAlias www.*.org.au
UseCanonicalName Off
VirtualDocumentRoot /path/to/sites/%2/pub
Hope that helps someone!
希望对某人有所帮助!
I read the docs on "Directory Name Interpolation" in mod_vhost_alias docs.
我在mod_vhost_alias docs 中阅读了有关“目录名称插值”的文档。
回答by Armel Larcier
I finally found a configuration that allows flexible subdomain creation.
我终于找到了一个允许灵活创建子域的配置。
See apache docs on mod_vhost_alias
请参阅mod_vhost_alias 上的apache 文档
If your root dev domain has 3 parts like dev.example.com you can use %-4+as a placeholder for everything before the root domain. If it has 4 parts, use %-5+.
如果您的根开发域有 3 个部分,例如 dev.example.com,您可以将其%-4+用作根域之前所有内容的占位符。如果它有 4 个部分,请使用%-5+.
<VirtualHost *:80>
VirtualDocumentRoot "/var/www/%-4+/webroot"
ServerName www.dev.example.com
ServerAlias *.dev.example.com
php_admin_value auto_prepend_file /var/www/setdocroot.php
</VirtualHost>
This way you can create a directory named /var/www/sub.domain/webrootand access it with the url sub.domain.dev.example.com.
通过这种方式,您可以创建一个名为/var/www/sub.domain/webrooturl的目录并访问它sub.domain.dev.example.com。
The line php_admin_value auto_prepend_file /var/www/setdocroot.phpfixes the docroot on some systems like OSX 10.9+
该行php_admin_value auto_prepend_file /var/www/setdocroot.php修复了某些系统上的 docroot,如 OSX 10.9+
Here is the content of setdocroot.php:
以下是内容setdocroot.php:
<?php
$_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);
?>
回答by Svetoslav Marinov
What I am noticing with this configuration is that $_SERVER['DOCUMENT_ROOT'] is pointing to /var/www/html and not to the vhost's doc root. weird.
我注意到这个配置是 $_SERVER['DOCUMENT_ROOT'] 指向 /var/www/html 而不是 vhost 的文档根。奇怪的。
Update (2010-07-24):
更新 (2010-07-24):
I just wrote a blog post how to setup your http proof server http://www.devcha.com/2010/07/how-to-setup-your-http-proof-server.html
我刚刚写了一篇博客文章如何设置你的 http 证明服务器 http://www.devcha.com/2010/07/how-to-setup-your-http-proof-server.html

