Apache/2.4.6(CentOS)
有一段时间,我为我的 Apache 服务器设置了一个捕获所有信息的页面,因此除非机器人有我的域名,否则它们就不会抓取我的主站点。
然而最近,我注意到这不再适用于我的配置。当通过 IP 地址(假设为 10.20.20.10)加载页面时,我得到的是主站点(假设为 mysite.net),而不是 catchall。
配置如下:
<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)
我找到了一种方法,让 catchall 默认加载,但这要求我将 catchall 更改为与主虚拟主机相同的监听 IP。这不是最理想的解决方案。我想将所有 vhost 更改为 * 也可以做到这一点,但这也不理想。
根据观察,httpd 似乎更喜欢更接近的匹配,并且会采用 IP 匹配而不是“*”。有人能解释一下为什么 Apache 不加载第一个虚拟主机以及如何修复这个问题吗?
答案1
mysite.net
我只是在这里想知道,由于它是第一个虚拟主机,VirtualHost 10.20.20.10:80
因此它成为默认站点是否有意义?
我不知道为什么_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 的工作方式。* 无法覆盖 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
祝你好运