虚拟主机分类
1、基于IP的虚拟主机
2、基于域名的虚拟主机
对于不同情况下配置apache虚拟主机的方法,一共八种情况。首先查看你虚拟主机属于哪一种情况,然后对号入座就好了。
1
基于ip地址的虚拟主机
Listen 80DocumentRoot /home/httpd/html1
ServerName www.ok1.com
ErrorLog /usr/local/apache/logs/error1_log
CustomLog /usr/local/apache/logs/access1_log combinedDocumentRoot /home/httpd/html2
ServerName www.ok2.com
ErrorLog /usr/local/apache/logs/error2_log
CustomLog /usr/local/apache/logs/access2_log combined2
基于IP 和多端口的虚拟主机配置
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080DocumentRoot /www/example1-80
ServerName www.example1.comDocumentRoot /www/example1-8080
ServerName www.example1.comDocumentRoot /www/example2-80
ServerName www.example1.orgDocumentRoot /www/example2-8080
ServerName www.example2.org3
单个IP 地址的服务器上基于域名的虚拟主机配置
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80DocumentRoot /www/example1
ServerName www.example1.com
ServerAlias example1.com. *.example1.com
# Other directives hereDocumentRoot /www/example2
ServerName www.example2.org
# Other directives here4
在多个IP 地址的服务器上配置基于域名的虚拟主机
Listen 80
# This is the "main" server running on 172.20.30.40
ServerName server.domain.com
DocumentRoot /www/mainserver
# This is the other address
NameVirtualHost 172.20.30.50DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here ...DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here ...5
在不同的端口上运行不同的站点(基于多端口的服务器上配置基于域名的虚拟主机)
Listen 80
Listen 8080
NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080ServerName www.example1.com
DocumentRoot /www/domain-80ServerName www.example1.com
DocumentRoot /www/domain-8080ServerName www.example2.org
DocumentRoot /www/otherdomain-80ServerName www.example2.org
DocumentRoot /www/otherdomain-80806
基于域名和基于IP 的混合虚拟主机的配置
Listen 80
NameVirtualHost 172.20.30.40DocumentRoot /www/example1
ServerName www.example1.comDocumentRoot /www/example2
ServerName www.example2.orgDocumentRoot /www/example3
ServerName www.example3.net