Apache-2.2

VirtualHost 的伺服器名稱不起作用(顯示 /var/www),所有別名都起作用

  • December 30, 2014

我已經安裝了 apache 2 並創建並啟用了一個虛擬主機。

虛擬主機有: ServerName example.com ServerAlias *.example.com ServerAlias foo.com ServerAlias *.foo.com ServerAlias bar.com ServerAlias *.bar.com

如果我將瀏覽器定向到www.example.com、test.example.com、foo.com、www.foo.com 等,除了裸伺服器名稱之外的任何內容,它都會按預期工作,顯示 /srv/www 的內容/example.com/public_html/index.php 是虛擬主機的 DocumentRoot。

但是,如果我在瀏覽器中只寫“ example.com ”,我會看到**/var/www/index.html的內容**

它沒有記憶體在瀏覽器中,我已經清除了記憶體並嘗試了另一個瀏覽器並嘗試通過代理。

這對我來說毫無意義。任何的想法?

以下是 /etc/apache2/sites-enabled/000-default 的內容

<VirtualHost *:80>
       ServerAdmin webmaster@localhost

       DocumentRoot /var/www
       <Directory />
               Options FollowSymLinks
               AllowOverride None
       </Directory>
       <Directory /var/www/>
               Options Indexes FollowSymLinks MultiViews
               AllowOverride None
               Order allow,deny
               allow from all
       </Directory>

       ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
       <Directory "/usr/lib/cgi-bin">
               AllowOverride None
               Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
               Order allow,deny
               Allow from all
       </Directory>

       ErrorLog ${APACHE_LOG_DIR}/error.log

       # Possible values include: debug, info, notice, warn, error, crit,
       # alert, emerg.
       LogLevel warn

       CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

以下是 /etc/apache2/sites-enabled/example.com 的內容:

   <VirtualHost *:80>
   ServerName example.com
   ServerAlias *.example.com
   ServerAlias foo.com
   ServerAlias *.foo.com
   ServerAlias bar.com
   ServerAlias *.bar.com
   #... and a few others
   DocumentRoot /srv/www/example.com/public_html
   ErrorLog /srv/www/example.com/error.log
   # Possible values include: debug, info, notice, warn, error, crit,
   # alert, emerg.
   LogLevel notice
   CustomLog /srv/www/example.com/access.log combined

     <Directory /srv/www/example.com/public_html>
       Options FollowSymLinks
       AllowOverride All
     </Directory>

   </VirtualHost>

我不明白為什麼使用裸伺服器名,預設虛擬主機優於與伺服器名稱特別匹配的虛擬主機,而所有別名都可以正常工作。

這是# apache2ctl -S 的輸出

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:443                  is a NameVirtualHost
    default server example.com (/etc/apache2/sites-enabled/default-ssl:2)
    port 443 namevhost example.com (/etc/apache2/sites-enabled/default-ssl:2)
*:80                   is a NameVirtualHost
    default server example.com (/etc/apache2/sites-enabled/000-default:1)
    port 80 namevhost example.com (/etc/apache2/sites-enabled/000-default:1)
    port 80 namevhost example.com (/etc/apache2/sites-enabled/example.com:1)
Syntax OK

(我想不用說我已經用“example.com”、“foo.com”和“bar.com”替換了實際的域名,只是為了在 SF 上發布)

顯然,文件並不清楚這一點。

來自https://issues.apache.org/bugzilla/show_bug.cgi?id=57384

“如果你省略了 ServerName

$$ in the virtual host definition $$,它是根據系統主機名計算的(系統主機名的雙重反向 DNS 查找)”。因此,如果啟用的站點 000-default 沒有伺服器名稱,則它就像它具有 example.com 作為 ServerName,例如.com 我的伺服器的主機名。 因此,在我的情況下,當客戶端請求域 example.com 時,000-default 是與請求的主機名匹配的第一個伺服器,並“擷取”請求。

如果我希望(像我一樣)讓預設站點擷取所有與任何伺服器名稱不匹配的請求,我需要在預設的 VirtualHost 定義中指定一個虛擬的 ServerName。這樣,對伺服器主機名的請求不會匹配預設虛擬主機,但會匹配 example.com 虛擬主機。不匹配任何伺服器名或別名的請求將被預設站點擷取,因為它是第一個。

“第一個列出的虛擬主機會擷取與伺服器名/伺服器別名不匹配的任何內容,即使您指定了伺服器名也是如此。”

/etc/apache2/sites-enabled/ 中連結了哪些其他文件?

通常,啟用 mod-info 對診斷目的很有用,如下所示(來自http://httpd.apache.org/docs/2.2/mod/mod_info.html):

<Location /server-info>
 SetHandler server-info
</Location>

<Location /server-info>
 SetHandler server-info
 Order deny,allow
 Deny from all
 Allow from yourcompany.com
</Location> 

通常我用證書來保護它,但這超出了這個解釋。一旦你啟用了它,並限制為 127.0.0.1(通過 ssh 連接到伺服器的隧道,設置你的主機文件等)。您可以看到 Apache 解析的確切執行配置。您可能會看到 example.com 在其他地方被引用。通常,當您使用 mod-info 時,事情會更有意義。

引用自:https://serverfault.com/questions/638416