
我的伺服器資訊是
Server version: Apache/2.4.6 (CentOS)
Server built: Nov 19 2015 21:43:13
我正在嘗試為 2 個不同的網站配置虛擬主機:biz.example.com 和 pin.example.com,它們託管在同一台伺服器上。 “var/www/html/”下方有兩個不同的資料夾,分別名為“biz”和“pin”,其中包含上述兩個網站的受人尊敬的專案文件。我正在嘗試按以下方式配置它。
在/etc/hosts下面的設定中
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
xxx.xxx.xxx.xxx biz.example.com
xxx.xxx.xxx.xxx pin.example.com
xxx.xxx.xxx.xxx 替換為伺服器 IP 位址。
在 /etc/httpd/conf/httpd.conf 中
IncludeOptional sites-enabled/*.conf
現在,在 /etc/httpd/sites-available 下有 biz.conf 和 pin.conf 檔案。我還在 /etc/httpd 下啟用了網站啟用資料夾,其中有 2 個檔案使用以下命令指向網站可用資料夾的 biz.conf 和 pin.conf
ln -s /etc/httpd/sites-available/biz.conf /etc/httpd/sites-enabled/biz.conf
ln -s /etc/httpd/sites-available/pin.conf /etc/httpd/sites-enabled/pin.conf
biz.conf 有以下內容
<VirtualHost *:80>
ServerName http://biz.example.com/
ServerAlias http://biz.example.com/
DocumentRoot "/var/www/html/biz"
<directory "/var/www/html/biz">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from 127.0.0.1
</directory>
</VirtualHost>
pin.conf 檔案中的配置被提到為
<VirtualHost *:80>
ServerName http://pin.example.com/
ServerAlias http://pin.example.com/
DocumentRoot "/var/www/html/pin"
<directory "/var/www/html/pin">
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from 127.0.0.1
</directory>
</VirtualHost>
在此設定中,如果我嘗試訪問http://biz.example.com/,正在載入正確的網站(商業網站)。但如果我嘗試訪問http://pin.example.com/,然後也會載入商務網站而不是固定網站。多個配置無法協同工作。
我還嘗試將 biz.conf 和 pin.conf 的虛擬配置合併到單一檔案 biz.conf 中,但效果不佳。
答案1
答:
1) 必須刪除 ServerName 和 ServerAlias 中的尾部斜杠
2)在這裡,我們可以刪除ServerAlias,而ServerName和ServerAlias都是相同的。
答案2
從路徑中刪除雙引號
DocumentRoot /var/www/html/pin
<directory /var/www/html/pin>
答案3
在此設定中,如果我嘗試訪問http://biz.example.com/,正在載入正確的網站(商業網站)。但如果我嘗試訪問http://pin.example.com/,然後也會載入商務網站而不是固定網站。
這是因為ServerName
和ServerAlias
指令不符(語法錯誤),在這種情況下,第一個定義的指令會VirtualHost
取得所有請求。
文件中使用非常相似的配置描述了該行為:
在單一 IP 位址上執行多個基於名稱的網站 (httpd.apache.org/docs/2.4/vhosts/examples.html)
# Ensure that Apache listens on port 80 Listen 80 <VirtualHost *:80> DocumentRoot "/www/example1" ServerName www.example.com # Other directives here </VirtualHost> <VirtualHost *:80> DocumentRoot "/www/example2" ServerName www.example.org # Other directives here </VirtualHost>
星號符合所有位址,因此主伺服器不處理任何請求。由於 的虛擬主機
ServerName www.example.com
在設定檔中排在第一位,因此它的優先順序最高,可以看作是預設或者基本的伺服器.這意味著,如果收到的請求與指定的指令之一不匹配ServerName
,則該請求將由該first 提供服務<VirtualHost>
。
解決方案:
必須
ServerName
沒有http://
前綴且沒有尾部斜杠,即ServerName biz.example.com
和
ServerName pin.example.com
ServerAlias
可以刪除,因為它具有相同的值ServerName
<Directory>
並且</Directory>
應該以大寫字母開頭舊的 Apache 2.2 存取控制語法應變更為新的 Apache 2.4
Require
語法。Order Deny,Allow Allow from 127.0.0.1
應替換為
Require local
看