Apache回应请求它不应该

我有以下2个域的logging

website.com 10.0.0.1 www.website.com 10.0.0.1 

我有(有)以下2个虚拟主机

 #note how the IP address is wrong <VirtualHost 10.0.0.2:80> ServerName website.com Redirect / http://www.website.com </VirtualHost> <VirtualHost 10.0.0.1:80> ServerName www.website.com #note how there is no alias here DocumentRoot /var/www/www.website.com <IfModule mpm_itk_module> AssignUserId www-website www-website </IfModule> CustomLog /var/log/apache2/www.website.com-access.log combined ErrorLog /var/log/apache2/www.website.com-error.log </VirtualHost> <VirtualHost 10.0.0.1:443> ServerName www.website.com DocumentRoot /var/www/www.website.com <IfModule mpm_itk_module> AssignUserId www-website www-website </IfModule> SSLEngine On SSLCertificateFile /etc/apache2/ssl/www.website.com CustomLog /var/log/apache2/www.website.com-ssl-access.log combined ErrorLog /var/log/apache2/www.website.com-ssl-error.log </VirtualHost> 

我预计http://website.com的请求将返回一个404。但是,它被认为是从http://www.website.com交付。 为什么?

LazyOne完全正确。 只要你有你的NameVirtualHost指令整理出来,那么使用为请求的IP地址定义的第一个VirtualHost ,如果没有其他ServerNameServerAlias匹配存在。

因此,如果要为每个没有匹配主机头匹配的IP地址提供404服务,请创build一个没有ServerNameServerAlias指令的VirtualHost (每个IP),并将其放置在configuration中,以使其首先加载。 就像是:

 <VirtualHost 10.0.0.1:80> Redirect 404 / </VirtualHost> <VirtualHost 10.0.0.2:80> Redirect 404 / </VirtualHost> <VirtualHost 10.0.0.2:80> ServerName website.com Redirect / http://www.website.com </VirtualHost> <VirtualHost 10.0.0.1:80> ServerName www.website.com #note how there is no alias here DocumentRoot /var/www/www.website.com ... </VirtualHost> 

更新 :下面的大部分是从apache2文档, 在这里和这里 cribbed。

主configuration文件(通常名为httpd.conf)首先被加载。 但是如果你在一个基于debian的系统上使用二进制包,很可能会被称为apache2.conf。 其他configuration文件是使用主configuration中的Include指令添加的。 Include指令的多个用途是允许的。 Include指令可以使用fnmatch样式通配符按字母顺序一次加载多个configuration文件。

为了进一步阐明(希望),你的主要configuration首先被加载。 当遇到Include指令时,它们按照它们在主configuration中出现的顺序加载。 如果一个单独的Include使用通配符,则每个匹配的configuration文件按字母顺序加载。

在debian服务器上,apache2.conf可能如下所示:

 # Include module configuration: # ... Include mods-enabled/*.conf # Include all the user configurations: Include httpd.conf # Include ports listing Include ports.conf # ... # Include generic snippets of statements Include conf.d/ # Include the virtual host configurations: Include sites-enabled/ 

换句话说,在mods-enabled /中以.conf结尾的任何文件都会在httpd.conf之前加载,而在httpd.conf之前加载的是在conf.d /中的任何文件之前加载的ports.conf。