假设服务器 IP 为 192.168.1.104
现在我们希望在访问 http://192.168.1.104 时访问的是 /var/www/html/vhost-1 中的内容,在访问 http://192.168.1.104:9999 时访问的是 /var/www/html/vhost-2 中的内容,配置方法如下:
编辑 /etc/httpd/conf/httpd.conf 文件(CentOS, Fedora Linux),在底下添加:
[Plain Text] 纯文本查看 复制代码 <VirtualHost 192.168.1.104:80>
ServerAdmin webmaster@groad.net
DocumentRoot /var/www/html/vhost-1
ServerName 192.168.1.104:80
<Directory /var/www/html/vhost-1>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Listen 9999
<VirtualHost 192.168.1.104:9999>
ServerAdmin webmaster@groad.net
DocumentRoot /var/www/html/vhost-2/
ServerName 192.168.1.104:9999
<Directory /var/www/html/vhost-2>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
上面最重要的一条是添加 Listen 9999 这一句,这里不需要再添加 Listen 80 这么一句了,因为配置文件中默认已有该句话。
对于虚拟主机 vhost-1 没有列出目录的功能,而 vhost-2 则有列出目录的功能,这是因为 vhost-2 中使用了 Options Indexes 命令。
分别往 vhost-1 和 vhost-2 中创建 index.html 文件,其内容分别为 “hello vhost-1-80port“ 和 "hello vhost-2-9999port",测试运行时可以看到:
注意,在 vhost-2 中拥有 index.html 文件时,服务器会默认去解析该文件,而不会列出目录,去掉 "index.xxx" 这些服务器默认解析的文件,就可以看到列出的目录。还需要注意,需要将/etc/httpd/conf.d/welcome 中的 -Indexex 属性改为 Indexex ,否则也不会默认列出目录,而是显示默认的欢迎主页。 |