首页 文章

为什么在Apache中设置虚拟主机后,http:// localhost会重定向到我的默认虚拟主机?

提问于
浏览
6

这可能是一个简单的问题,但我想更好地理解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>

现在当我去http://localhost时,我得到了site1的默认页面 .

我很了解它 . 我原以为只有明确要求http://site1.test的请求才能通过该指令进行路由,并且它不会成为默认值 . 有人可以解释为什么它成为默认值 .

4 回答

  • 0

    http://httpd.apache.org/docs/1.3/vhosts/name-based.html

    (对于2.x也应如此)

    “如果找不到匹配的虚拟主机,则将使用与IP地址匹配的第一个列出的虚拟主机 .

    因此,第一个列出的虚拟主机是默认虚拟主机 . 当IP地址与NameVirtualHost指令匹配时,永远不会使用主服务器的DocumentRoot . 如果您希望对与任何特定虚拟主机不匹配的请求进行特殊配置,只需将该配置放在容器中,然后将其列在配置文件中 . “

  • 2

    回答1是正确的,我将添加namevirtualhosts,因为第一个条目基本上匹配任何未命名的其他虚拟主机

    它应该只用于捕捉无意识的错误形成和破坏的流量

    例如,一个名为john.domain.com的machene运行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>
    
  • 11

    确认对于Apache 2.x,如果找不到匹配的虚拟主机,将使用第一个虚拟主机(具有相同的端口号) .

    http://httpd.apache.org/docs/2.2/vhosts/details.html

    “如果找不到匹配的vhost,请求将从第一个vhost提供,该端口号与客户端连接的IP列表中的匹配端口号”

    您始终可以在下面添加此代码,将其放在 NameVirtualHost *:80 下方,以便在找不到其他虚拟主机时默认提供默认文档根目录 .

    <VirtualHost *:80>
        ServerName localhost
        DocumentRoot /my/default/document/root
    </VirtualHost>
    
  • 4

    一种方法是:

    • 在VirtualHosts配置中,输入要启用的特定本地站点名称而不是使用通配符: <VirtualHost site1.dev:80> 而不是 <VirtualHost *:80>

    • 关闭 NameVirtualHost *:80 ,这可以通过在vhosts.conf文件中注释掉来完成

    • 在/ etc / hosts文件中提及环回IP的两个别名: 127.0.0.1 localhost site1.dev

    而已 . 您应该看到localhost像往常一样转到默认的DocumentRoot,并且site1.dev转到您已设置为虚拟主机的站点 .

相关问题