多注意的话
可以发现有些网址非常特别
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请多指教