Apache 2.2 忽略 VirtualDocumentRoot VirtualHosts?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/785966/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:31:30  来源:igfitidea点击:

Apache 2.2 ignoring VirtualDocumentRoot VirtualHosts?

apache2virtualhostapachehttpd.conf

提问by Mike

I have several domains that I would like to have wildcard subdomains enabled for through mod_vhost_alias

我有几个域希望通过 mod_vhost_alias 启用通配符子域

Included in my httpd.conf I have the following generalized rules:

我的 httpd.conf 中包含以下通用规则:

<VirtualHost [ip here]:80>
    ServerName domain1.com
    ServerAlias www.domain1.com
    DocumentRoot /home/user1/public_html
</VirtualHost>
<VirtualHost [ip here]:80>
    ServerName *.domain1.com
    VirtualDocumentRoot /home/user1/subdomains/%-3+
</VirtualHost> 

<VirtualHost [ip here]:80>
    ServerName domain2.com
    ServerAlias www.domain2.com
    DocumentRoot /home/user2/public_html
</VirtualHost>
<VirtualHost [ip here]:80>
    ServerName *.domain2.com
    VirtualDocumentRoot /home/user2/subdomains/%-3+
</VirtualHost>

The problem is that apache is completely ignoring the virtualhosts with the wildcard ServerNames. Any request for test.domain1.com or test.domain2.com will just show the contents of /home/user1/public_html (the default rule according to apache).

问题是 apache 完全忽略了带有通配符 ServerNames 的虚拟主机。任何对 test.domain1.com 或 test.domain2.com 的请求只会显示 /home/user1/public_html 的内容(根据 apache 的默认规则)。

Some other information:

一些其他信息:

/home/user1/subdomains/testand /home/user2/subdomains/testboth exist and have files in them

/home/user1/subdomains/test并且/home/user2/subdomains/test两者都存在并且其中有文件

All my domains have a wildcard subdomain listed in bind config, and they are pointing to the same IP specified in the Vhost rules. Standard Vhost rules for subdomains work, but not wildcard.

我所有的域在绑定配置中都有一个通配符子域,它们指向 Vhost 规则中指定的同一个 IP。子域的标准 Vhost 规则有效,但不能通配符。

A snipped version of the output of httpd -S

输出的剪辑版本 httpd -S

# httpd -S
VirtualHost configuration:
[ip here]:80      is a NameVirtualHost
         default server domain1.com (/etc/httpd/sites/user1:1)
         port 80 namevhost domain1.com (/etc/httpd/sites/user1:1)
         port 80 namevhost *.domain1.com (/etc/httpd/sites/user1:14)
         port 80 namevhost domain2.com (/etc/httpd/sites/user2:1)
         port 80 namevhost *.domain2.com (/etc/httpd/sites/user2:14)
Syntax OK
# httpd -M
Loaded Modules:
 core_module (static)
 mpm_prefork_module (static)
 http_module (static)
 so_module (static)
 rewrite_module (shared)
 auth_basic_module (shared)
 authz_host_module (shared)
 include_module (shared)
 log_config_module (shared)
 logio_module (shared)
 mime_magic_module (shared)
 mime_module (shared)
 vhost_alias_module (shared)
 dir_module (shared)
 suexec_module (shared)
 php5_module (shared)
 suphp_module (shared)
 ssl_module (shared)
Syntax OK

Any suggestions as to what the problem is would be greatly appreciated.

任何有关问题所在的建议将不胜感激。

回答by Mike

I'm such a noob, it was a real simple solution.

我是个菜鸟,这是一个非常简单的解决方案。

ServerNamedoes not allow wildcards, but ServerAliasdoes, so I just duplicated the servername again.

ServerName不允许通配符,但允许ServerAlias,所以我只是再次复制了服务器名称。

Here is the final minimized config for domains anyone who happens to find this answer (I hate it when you search on google and find questions but no answers):

这是碰巧找到此答案的任何人的域的最终最小化配置(当您在 google 上搜索并找到问题但没有答案时,我讨厌它):

<VirtualHost [ip here]:80>
    ServerName domain1.com
    ServerAlias www.domain1.com
    DocumentRoot /home/user1/public_html
</VirtualHost>
<VirtualHost [ip here]:80>
    ServerName domain1.com #<-- The fix. Wildcards not supported in ServerName
    ServerAlias *.domain1.com
    VirtualDocumentRoot /home/user1/subdomains/%-3+
</VirtualHost>

回答by Alex Yaroshevich

David, this solution that stops mod_rewrite cycle when success will be useful for your problem).

大卫,这个解决方案在成功时停止 mod_rewrite 循环对您的问题有用)。

Rewrite round-robin stopper:

重写循环停止器:

RewriteCond %{ENV:REDIRECT_STATUS} 200|[45]0[0-9]
RewriteRule .* - [L]