多注意的話
可以發現有些網址非常特別
google.com...
mail.google.com
picasaweb.google.com而且 ping 一下還會發現這些 DomainName 都連到同一個 IP
這就是 Apache Virtual Host 的功能啦
當然 Google 不是這樣做的啦………舉例而已要啟動 Apache 的 Virtual Host 功能
首先開啟 /usr/local/etc/apache22/httpd.conf
在任意地方加入以下設定
複製程式
# Virtual hosts
Include etc/apache22/extra/httpd-vhosts.conf
由上面的設定可以看出
這一段會使 Apache 去讀取 extra/httpd-vhosts.conf
而 httpd-vhosts.conf 就是 Virtual Host 的設定檔
當然也可以直接寫在 http.conf 內
但是為了方便管理以及日後的維護
通常都會放在 extra/httpd-vhosts.conf接下來看看 httpd-vhosts.conf 裡面寫了些什麼
複製程式
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at
# <URL:[url]http://httpd.apache.org/docs/2.2/vhosts/>[/url]
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
VirtualHost example:
Almost any Apache directive may go into a VirtualHost container.
The first VirtualHost section is used for all requests that do not
match a ServerName or ServerAlias in any <VirtualHost> block.
<VirtualHost *:80>
ServerAdmin [email]webmaster@dummy-host.example.com[/email]
DocumentRoot /www/docs/dummy-host.example.com
ServerName dummy-host.example.com
ServerAlias [url]www.dummy-host.example.com[/url]
ErrorLog /var/log/dummy-host.example.com-error_log
CustomLog /var/log/dummy-host.example.com-access_log common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email]webmaster@dummy-host2.example.com[/email]
DocumentRoot /www/docs/dummy-host2.example.com
ServerName dummy-host2.example.com
ErrorLog /var/log/dummy-host2.example.com-error_log
CustomLog /var/log/dummy-host2.example.com-access_log common
</VirtualHost>
可以看出每個不同的 vhost 都被包在標籤中
<VirtualHost *:80>
......
</VirtualHost>
參數說明:
ServerAdmin:此 vhost 的管理員電子郵件
DocumentRoot:連結到此 vhost 時,會以哪個目錄當作預設的根目錄
ServerName:vhost 的 DNS
ServerAlias:vhost 的 Alias
ErrorLog:對於這個 vhost 錯誤記錄檔的儲存位置
CustomLog:對於這個 vhost 記錄檔的儲存位置
設定好之後
別忘記將原本做為參考用的二個 vhost 刪除或設為註解
然後重新啟動 appachectl restart
//----------------------------------------------------------------
再補上一招管理密技
用的方法一樣是「include」
你把的所有 vhost 設定分別儲存成檔案存放在其他目錄
複製程式
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin [email]mail@mail.com[/email]
DocumentRoot /usr/local/www/apache22/data/
ServerName 192.168.0.1
ErrorLog /var/log/httpd-error.log
CustomLog /var/log/httpd-access.log combined
<Directory />
AllowOverride None
Order Deny,Allow
Deny from all
</Directory>
<Directory "/usr/local/www/apache22/data/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Include /usr/local/etc/apache22/vhosts/*.conf
然後其他的設定檔分別儲存到 /usr/local/etc/apache22/vhosts/
檔名為
vhost1.mychat.com.conf vhost2.mychat.com.confvhost1.mychat.com.conf:複製程式
<VirtualHost *:80>
ServerAdmin [email]mail1@mychat.com[/email]
DocumentRoot /usr/local/www/apache22/data/vhost1/
ServerName vhost1.mychat.com.conf
ErrorLog /var/log/httpd-error.log
CustomLog /var/log/httpd-access.log combined
<Directory />
AllowOverride None
Order Deny,Allow
Deny from all
</Directory>
<Directory "/usr/local/www/apache22/data/vhost1/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
vhost2.mychat.com.conf複製程式
<VirtualHost *:80>
ServerAdmin [email]mail2@mychat.com[/email]
DocumentRoot /usr/local/www/apache22/data/vhost2/
ServerName vhost2.mychat.com.conf
ErrorLog /var/log/httpd-error.log
CustomLog /var/log/httpd-access.log combined
<Directory />
AllowOverride None
Order Deny,Allow
Deny from all
</Directory>
<Directory "/usr/local/www/apache22/data/vhost2/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
這樣管理上會方便許多
原文:
Zeroplex 生活隨筆: FreeBSD Apache Virtual Hosthttp://zeroplex.blogspot.com/2008/05/...irtual-host.html請多指教