Apache服务器虚拟主机设置问题如何使通过IP访问为默认的apache页面 <转载>

原文链接:http://www.douban.com/note/28257219/

这也可以参考解决虚拟主机和宿主主机的同IP不同域名的冲突问题

============================================================

httpd.conf修改的内容为: # Virtual hosts Include conf/extra/httpd-vhosts.conf (去掉注释) httpd-vhosts.conf文件配置如下: NameVirtualHost *:80 <VirtualHost *:80> ServerAdmin webmaster@www.charity.org DocumentRoot /www/docs/www.charity.org ServerName www.charity.org ServerAlias www.charity.org*. charity.org ErrorLog logs/www.charity.org-error_log CustomLog logs/www.charity.org-access_log common </VirtualHost> <VirtualHost *:80> ServerAdmin webmaster@www.charity.org DocumentRoot /www/docs/www.test.com/ ServerName www.test.com ErrorLog logs/www.test.com-error_log CustomLog logs/www.test.com-access_log common </VirtualHost> 配置完毕,分别在/www/docs/www.charity.org, /www/docs/www.test.com/,下创建index.html文件。 重起httpd服务: $/usr/local/apache2/bin/apachectl restart 然后访问虚拟主机: http://www.charity.org http://www.test.com 均显示正常,试着通过IP访问,结果出现与http://www.charity.org显示相同的页面。 apache网站解释为当一个请求到达的时候,服务器会首先检查它是否使用了一个能和NameVirtualHost相 匹配的IP地址。如果能够匹配, 它就会查找每个与这个IP地址相对应的<VirtualHost>配置段, 并尝试找出一个ServerName或ServerAlias配置项与请求的主机名相同的。 如果找到了,它就会使用这个服务器的配置。 否则,将使用符合这个IP地址的第一个列出的虚拟主机。 如果要通过IP访问为默认的apache页面 使用下面的配置文件片段,用ip和用两个不同的域名访问会出来的页面都不一样(bumpy在/etc/hosts里就是127.0.0.1): NameVirtualHost * <VirtualHost *> ServerName 127.0.0.1 DocumentRoot /var </VirtualHost> <VirtualHost *> ServerName bumpy DocumentRoot /var/log </VirtualHost> <VirtualHost *> ServerName localhost DocumentRoot /var/www </VirtualHost>
原文地址:https://www.cnblogs.com/kanone/p/2282753.html