默認虛擬主機就是配置文件裏的第一個虛擬主機。關於默認虛擬主機有個特色,凡是解析到這臺機器的域名,不論是什麼域名,只要在配置文件中沒有配置,那麼都會訪問到這個虛擬主機上來。 舉例,咱們直接用 ip 訪問,會訪問到這個站點上來。爲了不別人亂解析,因此應該把默認也就是第一個虛擬主機給禁止掉(有2種方式:一種是指定一個空目錄,另外一種是使用deny allow語句)。html
這裏使用deny allow的方式:web
# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
把下面的配置:
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/local/apache2/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>
修改成:
apache
<VirtualHost *:80>
DocumentRoot "/usr/local/apache2/docs/default-vhost"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/default-vhost-error_log"
CustomLog "logs/default-vhost-access_log" common
<Directory />
Order deny,allow
Deny from all
</Directory>
</VirtualHost>vim
# mkdir -p /usr/local/apache2/docs/default-vhostcurl
# /usr/local/apache2/bin/apachectl -tide
Syntax OKthis
# /usr/local/apache2/bin/apachectl graceful
url
這時候,咱們用ip或者順便指定一個http頭去訪問,發現已經提示:
# curl -x localhost:80 www.baidu.com
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /
on this server.</p>
</body></html>
server