
若要直接查看我對問題的描述,請參閱問題部分。
情境:
運行 XUbuntu 14.10 和 Apache 2.4.10。
我對 Apache 很陌生,所以我正在嘗試設定簡單的 VH...取得了一些成功,但我仍然有一個問題。我閱讀了文檔,但找不到我的配置有什麼問題。我想配置兩個虛擬主機:我的名字.com和企鵝網。
配置
這是我目前配置的簡短描述(並非詳盡無遺)。
/etc/apache2/apache2.conf
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options +Indexes
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/html/flashlight/>
AllowOverride All
</Directory>
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
# Include generic snippets of statements
IncludeOptional conf-enabled/*.conf
# Include the virtual host configurations:
IncludeOptional sites-enabled/*.conf
/etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
<Directory /var/www/>
AuthName "Mon domaine"
AuthType Basic
AuthUserFile /etc/apache2/users.passwd
Options +Indexes
Options FollowSymLinks
AllowOverride None
Require valid-user
</Directory>
</VirtualHost>
/etc/apache2/sites-enabled/myname.conf
<VirtualHost 127.0.0.2:80>
ServerName myname.com
ServerAlias www.myname.com
DocumentRoot /var/www/html/myname
<Directory /var/www/html/myname>
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
/etc/apache2/sites-enabled/penguin.conf
<VirtualHost 127.0.0.3:80>
ServerName penguin.com
ServerAlias www.penguin.com
DocumentRoot /var/www/html/penguin
<Directory /var/www/html/penguin>
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
兩個虛擬主機均已依照 Apache 文件中所述使用a2ensite
. Apache 已重新啟動/etc/init.d/apache2 graceful
。/var/www/html/penguin
我/var/www/html/myname
建立了簡單的檔案來index.html
檢查我是否在虛擬主機上。
最後,我改為/etc/hosts
包含:
127.0.0.1 localhost
127.0.0.2 myname.com
127.0.0.3 penguin.com
為了檢查配置,我運行apachectl -S
並可以看到:
VirtualHost configuration:
127.0.0.3:80 penguin.com (/etc/apache2/sites-enabled/penguin.conf:1)
127.0.0.2:80 myname.com (/etc/apache2/sites-enabled/myname.conf:1)
*:80 www.example.com (/etc/apache2/sites-enabled/000-default.conf:1)
問題
現在我已經配置了兩個虛擬主機,我嘗試存取它們。當我嘗試penguin.com
在瀏覽器 (Firefox) 中訪問時,我看到的/var/www/html/index.html
是/var/www/html/penguin/index.html
.我必須明確地寫入penguin.com/index.html
才能看到正確的索引頁。
但是當我訪問時myname.com
,會提供正確的索引!
而且,當我使用 Chrome 造訪這些網站時,不會出現此問題,並且我直接看到正確的索引頁面。
我的 Apache 設定有問題嗎?或者您認為它僅與 Firefox 有關(也許它無法/etc/hosts
正確讀取)?
編輯
只是忘了提及,當我透過 IP (127.0.0.2/3) 存取頁面時,我在 Firefox 中遇到了同樣的問題(僅 127.0.0.2 有效)。
使用wget
,我按預期獲得了文件。所以我猜這可能與瀏覽器有關,而不是 Apache 配置。
編輯2
為了「解決」這個問題,我用我的本機 IP 位址取代了 /etc/hosts 中的環回位址。此外,我<VirtualHost *:80>
在兩個檔案中都替換了 VirtualHost 聲明。現在工作正常!
我仍然想知道為什麼 Firefox 在給出完整的環回 IP 位址時無法獲得正確的索引。
抱歉發了這麼長的貼文!
謝謝
答案1
在這種情況下,Firefox 將檢查您的 /etc/hosts 作為名稱解析的最後一個選項。
您可以嘗試這個解決方法。
- 開啟 Firefox 並輸入
about:config
URL。 - 搜尋
keyword.enabled
並將其設定為 False。 - 確保在 /etc/nsswitch.conf 中,
hosts
條目具有files
第一個選項。