当同一个端口上有多个站点时,Apache如何知道要加载哪个站点?

我已经在我的本地主机上设置了2个站点。 一个在localhost:8197和一个在fc.localhost:8197当然,为了做到这一点,我编辑了我的/ etc / hosts文件来包含

fc.localhost 127.0.0.1 

所以,这些是我的configuration分别为每个站点:

000-default.conf(位于localhost的站点):

 <VirtualHost *:8197> ServerAdmin webmaster@localhost DocumentRoot /var/www/html ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory /var/www/html/> Options +Indexes +FollowSymLinks +ExecCGI AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost> 

fc.conf(网站位于fc.localhost):

 <VirtualHost *:8197> ServerAdmin webmaster@localhost DocumentRoot /home/alex/Documents/fc/Website/fc/html ServerName fc.localhost ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined <Directory /home/alex/Documents/fc/Website/fc/html> Options +Indexes +FollowSymLinks +ExecCGI AllowOverride All Order allow,deny Allow from all Require all granted </Directory> </VirtualHost> 

假设我的本地IP地址是10.8.210.51,并且我的本地networking中的一些电脑在浏览器的地址栏中放置了10.8.210.51:8197,那么我身边的apache将服务于第一个站点(本地主机)而不是第二个站点fc.localhost)。 同样的事情发生,如果我把127.0.0.1:8197在我的地址栏(服务器端)。

所以,我的问题是,出于清楚的兴趣,Apache如何决定哪个站点服务,如何改变这种行为?

对你的具体情况的简短答案:apache将服务于第一个重叠的虚拟主机。 由于000-默认是在字母表中的fc.localhost之前,第一个虚拟主机被加载并提供服务。

要回答我所假设的是后续问题:您可以使用namedVirtualHosts在同一IP:PORT上托pipe多个站点

这项技术依赖于HTTP规范中的主机头 。

而且由于SSL https的虚拟主机可能通过服务器名称指示进行复杂化

这被称为“基于名称的虚拟主机”,在这里描述:

[ http://httpd.apache.org/docs/2.4/en/vhosts/name-based.html%5D