为什么在 Apache 中设置虚拟主机后 http://localhost 重定向到我的默认虚拟主机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/526832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does http://localhost redirect to my default virtual host once I setup virtual hosts in Apache?
提问by Bialecki
This is probably an easy question, but I want to understand better how Apache works with virtual hosts. I am setting up virtual hosts because I work on multiple websites at once and I don't want to use subdirectories. I was pretty much using the default Apache httpd.conf file with the DocumentRoot pointing to something like "/www". I uncommented the virtual hosts include and added the following:
这可能是一个简单的问题,但我想更好地了解 Apache 如何与虚拟主机一起工作。我正在设置虚拟主机,因为我同时在多个网站上工作并且我不想使用子目录。我几乎使用默认的 Apache httpd.conf 文件,其中 DocumentRoot 指向诸如“/www”之类的内容。我取消了虚拟主机的注释并添加了以下内容:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName site1.dev
DocumentRoot /www/site1
</VirtualHost>
<VirtualHost *:80>
ServerName site2.dev
DocumentRoot /www/site2
</VirtualHost>
Now when I go to http://localhostI get the default page for site1.
现在,当我访问http://localhost 时,我得到了 site1 的默认页面。
I'm sure there is a reason why this makes sense, but I don't quite understand it. I would've thought that only requests that were explicitly to http://site1.testwould get routed through that directive and it wouldn't just become the default. Can someone explain why it becomes the default.
我确信这是有道理的,但我不太明白。我原以为只有明确发送到http://site1.test 的请求才会通过该指令进行路由,而且它不会成为默认值。有人可以解释为什么它成为默认值。
回答by Joe
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
(Should be true for 2.x also)
(对于 2.x 也应该如此)
"If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
“如果找不到匹配的虚拟主机,则将使用与 IP 地址匹配的第一个列出的虚拟主机。
As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a container and list it first in the configuration file."
因此,列出的第一个虚拟主机是默认虚拟主机。当 IP 地址与 NameVirtualHost 指令匹配时,将永远不会使用来自主服务器的 DocumentRoot。如果您想为与任何特定虚拟主机不匹配的请求进行特殊配置,只需将该配置放入一个容器中,并在配置文件中首先列出它。”
回答by Alan Doherty
answer 1 is correct and i'd add with namevirtualhosts as the first entry essentially matches any not-named elsewhere virtualhost
答案 1 是正确的,我会添加 namevirtualhosts 作为第一个条目基本上匹配任何未命名的其他地方的虚拟主机
it should ONLY be used to catch unintentional mal-formed and broken traffic
它应该只用于捕获无意的畸形和中断的流量
ie a machene with one ip called john.domain.com running www.domain.com and www.domain2.com as valid webservers on ip www.xxx.yyy.zzz might have an optimal config like thus
即具有一个名为 john.domain.com 的 ip 的机器运行 www.domain.com 和 www.domain2.com 作为 ip www.xxx.yyy.zzz 上的有效网络服务器可能具有这样的最佳配置
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a directory readable by apache with only a robots.txt denying everything
ServerName bogus
ErrorDocument 404 "/errordocuments/unknown-name.html"
#custom 404 describing how/what they might have done wrong try pointing a browser {with a hosts file at http://bogus/ on 193.120.238.109 to see mine#
ErrorLog /var/log/httpd/unknown-error.log
CustomLog /var/log/httpd/unknown-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a possibly different directory readable by apache with only a robots.txt denying everything
ServerName www.xxx.yyy.zzz
ServerAlias john.domain.com
ErrorDocument 404 "/errordocuments/ip-name.html"
ErrorDocument 403 "/errordocuments/ip-name.html"
#custom 404 telling them as a likely hacker/bot you wish to have nothing to do with them see mine at http://193.120.238.109/
ErrorLog /var/log/httpd/ip-error.log
CustomLog /var/log/httpd/ip-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain.com
RedirectPermanent / http://www.domain.com/
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain
ServerName www.domain.com
ServerPath /domain
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
RedirectPermanent / http://www.domain2.com/
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain2
ServerName www.domain2.com
ServerPath /domain2
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
回答by Unnawut
Confirming that for Apache 2.x, the first virtual host (with the same port number) will be used if a matching virtual host is not found.
确认对于 Apache 2.x,如果找不到匹配的虚拟主机,将使用第一个虚拟主机(具有相同的端口号)。
http://httpd.apache.org/docs/2.2/vhosts/details.html
http://httpd.apache.org/docs/2.2/vhosts/details.html
"If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected"
“如果找不到匹配的虚拟主机,则从客户端连接的 IP 列表中具有匹配端口号的第一个虚拟主机提供请求”
You can always add this code below, put it right below NameVirtualHost *:80so that your default document root is served by default if no other virtual hosts found.
您始终可以在下面添加此代码,将其放在正下方,NameVirtualHost *:80以便在没有找到其他虚拟主机的情况下默认为您的默认文档根目录提供服务。
<VirtualHost *:80>
ServerName localhost
DocumentRoot /my/default/document/root
</VirtualHost>
回答by Meral
Simply put this code at top in httpd-vhosts.conf
只需将此代码放在 httpd-vhosts.conf 的顶部
<VirtualHost localhost:80>
ServerName localhost
DocumentRoot d:/xampp/htdocs
<Directory "d:/xampp/htdocs/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
回答by bhavinb
One way to do this is:
一种方法是:
In your VirtualHosts configuration, enter the specific local site name you want to enable instead of using a wildcard:
<VirtualHost site1.dev:80>instead of<VirtualHost *:80>Switch off
NameVirtualHost *:80which can be done by commenting it out in your vhosts.conf fileIn your /etc/hosts file mention both aliases for the loopback IP:
127.0.0.1 localhost site1.dev
在您的 VirtualHosts 配置中,输入您要启用的特定本地站点名称,而不是使用通配符:
<VirtualHost site1.dev:80>而不是<VirtualHost *:80>关闭
NameVirtualHost *:80可以通过在 vhosts.conf 文件中注释掉它来完成在您的 /etc/hosts 文件中提及环回 IP 的两个别名:
127.0.0.1 localhost site1.dev
That's it. You should see that localhost goes to the default DocumentRoot as usual and the site1.dev goes to the site you've setup as virtual host.
就是这样。您应该会看到 localhost 像往常一样转到默认的 DocumentRoot,而 site1.dev 转到您设置为虚拟主机的站点。

