Apache默认虚拟主机未按正确顺序加载

Apache / 2.4.6(CentOS)

有一段时间了,我已经为我的Apache服务器设置了一个catchall页面设置,所以漫游器不会抓取我的主站点,除非他们有我的域名。

不过最近,我注意到这不再适用于我的configuration。 当通过IP地址(比如10.20.20.10)加载页面时,我得到主站点(比如说mysite.net)而不是catchall。

configuration看起来像这样:

<VirtualHost _default_:80> ServerName default # More config ... </VirtualHost> <VirtualHost _default_:443> ServerName default # More config ... </VirtualHost> <VirtualHost 10.20.20.10:80> ServerName mysite.net # More config ... </VirtualHost> <VirtualHost 10.20.20.10:443> ServerName mysite.net # More config ... </VirtualHost> 

运行apachectl -S向我透露,它没有被加载为默认值:

 10.20.20.10:443 is a NameVirtualHost default server mysite.net (/etc/httpd/sites-enabled/01-mysite.conf:24) port 443 namevhost mysite.net (/etc/httpd/sites-enabled/01-mysite.conf:24) *:80 localhost (/etc/httpd/sites-enabled/00-catchall.conf:2) *:443 localhost (/etc/httpd/sites-enabled/00-catchall.conf:16) 

我能够find一种方法来默认加载我的全部内容,但是它要求我将我的全部内容更改为与我的主要虚拟主机相同的IP地址。 不是最理想的解决scheme。 我想可以把所有的虚拟主机改成*也可以,但是这也不是很理想。

根据观察,似乎httpd喜欢更接近的匹配,并采取“*”的IP匹配。 谁能阐明为什么Apache不加载第一个虚拟主机,什么可能解决这个问题?

我只是想知道在这里,是不是有道理,因为mysite.netVirtualHost 10.20.20.10:80 mysite.net的第一个虚拟主机,它将成为默认网站?

我不知道为什么_default_不适合你。 但也许这样的事情可以改为工作。 这是你说你已经尝试过了吗? 有什么不理想的呢?

 <VirtualHost 10.20.20.10:80> ServerName default # More config ... </VirtualHost> <VirtualHost 10.20.20.10:443> ServerName default # More config ... </VirtualHost> <VirtualHost 10.20.20.10:80> ServerName mysite.net # More config ... </VirtualHost> <VirtualHost 10.20.20.10:443> ServerName mysite.net # More config ... </VirtualHost> 

如果您使用的是Apache 2.2,则此问题/答案将解释您的问题。 但你不是。 老实说,这不是我过分熟悉的东西。 显然,你不应该使用_default_和基于名字的虚拟主机,而是使用*:**:port

在VirtualHost上下文中的_default _:*和*:*之间的区别

 So with a named based virtualhosting configuration: <Virtualhost *:80> with ServerName foo.com means "on all IPs managed on this host", "on port 80", "if the request host header is foo.com" I'll use this virtualhost <Virtualhost *:*> with Servername foo.com means "on all IPs managed on this host", "on all ports", "if the request host header is foo.com" I'll use this virtualhost <Virtualhost 10.0.0.2:*> with Servername foo.com means "for request incoming from my network interface 10.0.0.2", "on all ports", "if the request host header is foo.com" I'll use this virtualhost <Virtualhost _default_:*> with Servername foo.com : should not be used with name based virtualhosting And on an IP based Virtualhosting: <Virtualhost 10.0.0.2:*> means "I'll use this virtualhost for request coming on my 10.0.0.2 interface" <Virtualhost _default_:443> means "I'll use this virtualhost for all other network interface on my host for request coming on port 443" <Virtualhost _default_:*> means "I'll use this virtualhost for all other network interface on my host, if it is not matched by a previous rule, and if the request host header is not matched by a named based virtualhost" 

Ubuntu与你一样,我试图分配一个IP到一个虚拟主机(example2.com),而不是<VirtualHost *:80> ,它现在覆盖默认的虚拟主机。 看来这只是Apache的工作原理。 A *不能覆盖IP地址。 curl 192.168.1.143现在给我example2.com而不是catchall页面。

 $ sudo apachectl -S VirtualHost configuration: 192.168.1.143:80 example2.com (/etc/apache2/sites-enabled/example2.com.conf:1) *:80 is a NameVirtualHost default server www1.swass (/etc/apache2/sites-enabled/000-default.conf:1) port 80 namevhost www1.swass (/etc/apache2/sites-enabled/000-default.conf:1) port 80 namevhost example.com (/etc/apache2/sites-enabled/example.com.conf:1) wild alias *.example.com 

祝你好运