Apache配置完虛擬主機后,使用Chrome訪問localhost還是默認目錄htdocs


Chrome 解析DNS出錯,這個錯誤比較罕見,甚至說有點奇特。今天在使用Apache配置虛擬主機時,出現了一個非常奇怪的現象。我按照配置的步驟配置虛擬主機,如下

配置虛擬主機的步驟如下:

1. 啟用  httpd-vhosts.conf

在 httpd.conf 文件中 

# Virtual hosts ,虛擬主機

Include conf/extra/httpd-vhosts.conf

2. httpd-vhosts.conf文件中做配置

#配置我們自己的虛擬主機

<VirtualHost 127.0.0.1:80>

    DocumentRoot "d:/myblog"

    #這里配置歡迎首頁面 

    DirectoryIndex index.html index.htm index.php

    <Directory />

    Options FollowSymLinks

    #不許可別人修改我們的頁面

    AllowOverride None

    #設置訪問權限

    Order allow,deny

Allow from all

//由於版本更新,所以這個語句不能再使用,Apache2.4需要使用Require all granted

    </Directory>

</VirtualHost>

3.修改hosts 文件 

.127.0.0.1            localhost

但是很遺憾的是,我調試的瀏覽器是Chrome,這就使得我剛開始解決問題時就出現錯誤的方向,我剛開始認為是自己的配置錯誤,所以一直在嘗試,但是最后都沒有發現問題,Chrome瀏覽器停留在這個頁面。(這個頁面就是Apache的默認頁面,而不是虛擬主機中的頁面)

后來,我覺得會不會是hosts文件出錯呢?所以我嘗試使用IP:http://127.0.0.1/訪問,讓我驚訝的是,這時候訪問成功了。

所以我決定問題就出現在localhost與IP: http://127.0.0.1/之間,找到問題的發生點,然后我在百度搜索出來的結果如下
1、localhost也叫local ,正確的解釋是:本地服務器
 
127.0.0.1在windows等系統的正確解釋是:本機地址(本機服務器)
 
他們的解析通過本機的host文件,windows自動將localhost解析為127.0.0.1
 
2、localhot(local)是不經網卡傳輸!這點很重要,它不受網絡防火牆和網卡相關的的限制。
 
127.0.0.1是通過網卡傳輸,依賴網卡,並受到網絡防火牆和網卡相關的限制。
本機IP 也是通過網卡傳輸的,依賴網卡,並受到網絡防火牆和網卡相關的限制。
但是本機IP與127.0.0.1的區別是:
 
127.0.0.1 只能通過本機訪問
本機IP 通過本機訪問也能通過外部訪問
一般設置程序時本地服務用localhost是最好的,localhost不會解析成ip,也不會占用網卡、網絡資源。
但是即使找到這些區別我也沒法解決這個問題啊,因為我的配置在網上的資料中是正確的。那么,我覺得最關鍵的可能就是Apache的版本問題,但是搜索后也沒有出現這種情況。
這時候只剩下最后一種可能,那就是瀏覽器有問題,於是,我用了搜狗瀏覽器去訪問。結果顯示正常,終於知道問題在哪里了。就是Chrome瀏覽器出了問題。

確定問題來源后,我再次在百度查找答案,不過我找不到答案。我覺得主要原因可能是因為比較少人使用,而且不是主流,所以碰到這種問題比較少。所以只好翻牆出去,到谷歌找答案。
原來問題的關鍵在於dns的解析上,不過我真的很笨,dns就是瀏覽器解析的啊,肯定是瀏覽器出現了問題。
我把問題的解決方法告訴大家:

down vote
accepted

 

方法一:1Clear up the Chrome's DNS cache by typing this in the Chrome browser

  • chrome://net-internals/#dns

Screenshot -> Flushing Chrome DNS cache

  • You will see a button "Clear Host Cache". Press that DNS cache will be flushed.

  • Keep this DNS window open. Now access the virtual host in the browser for me it was http:/api.localhost. Once you do that you will see a new entry in the DNS window. for me it was "localhost." notice the period "." at the end of localhost that showed an error.

  • Last step is to simply add this entry as

    127.0.0.1 localhost.

    in the hosts file located at for ubuntu : /etc/hosts

    for windows : C:\Windows\System32\drivers\etc\hosts

方法二:Another solution could be to ditch the .localhost /.dev at the end of your local virtual host domain

This has to do with some new changes by google. ".dev" and ".local" comes under google's TLD (In the corner of the internet where people care about DNS, there is a bit of an uproar at Google's application for over a hundred new top-level domains, including .dev etc)

Use a domain name you own. Possibly using the full name like "localhost.dev.$yourdomain" could help here on the setup.

原來,谷歌瀏覽器升級之后,對DNS的解析產生了新的規則,如.localhost/.dev都屬於 google's TLD 中的一種了,所以,再也不能使用谷歌瀏覽器通過localhost訪問本地了。不過上述的方法一我在測試后發現還是不可行,猜測原因是隨着更高版本的升級,對DNS的解析更加嚴格了。所以在這里還是推薦使用方法二,也就是把localhost改為localhost.dev.$yourdomain"或者其他(只要不出現”.localhost“就可以了)。當然,這個需要在 windows :  C:\Windows\System32\drivers\etc\hosts中配置。
所以到這里,問題就完美解決了。
在這里再次提醒自己,遇到問題不要慌,深入分析問題,分析出問題屬於哪一類的問題,使用便於搜索的詞語描繪這個錯誤,在搜索引擎中搜索相應的解決辦法,如果還是解決不了,到谷歌上去搜,如果還是解決不了,可以到論壇上面發問,這樣,一般都不會出現解決不了的問題。stack overflow真的是一個好地方,我有很多問題都在這里找到解決的方法。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM