Home > php教程 > php手册 > body text

apache2.2 虚拟主机配置详解,apache2.2详解

WBOY
Release: 2016-06-13 08:50:45
Original
945 people have browsed it

apache2.2 虚拟主机配置详解,apache2.2详解

一、修改httpd.conf

打开appserv的安装目录,找到httpd.conf文件,分别去掉下面两行文字前面的#号。 

#LoadModule vhost_alias_module modules/mod_vhost_alias.so  
Copy after login

去掉#意思是启用apache的虚拟主机功能。

#Include conf/extra/httpd-vhosts.conf
Copy after login

去掉这一行的#意思是从conf/extra/httpd-vhosts.conf这个文件导入虚拟主机配置

二、修改httpd-vhosts.conf

打开该文件,看到以下类似内容。虚拟主机的配置也是参照以下内容配置。下面对该内容进行讲解

NameVirtualHost *:80  
  
<VirtualHost *:80>  
    ServerAdmin webmaster@dummy-host.www.phpStudy.net  
    DocumentRoot "C:/Program Files/Apache2/docs/dummy-host.www.phpStudy.net"  
    ServerName dummy-host.www.phpStudy.net  
    ServerAlias www.dummy-host.www.phpStudy.net  
    ErrorLog "logs/dummy-host.www.phpStudy.net-error.log"  
    CustomLog "logs/dummy-host.www.phpStudy.net-access.log" common  
</VirtualHost>  
Copy after login

NameVirtualHost *:80

注意1:NameVirtualHost 指定虚拟主机所使用的IP地址或域名,但是最好是IP地址。使用基于域名的虚拟主机时,NameVirtualHost是必要的指令。NameVirtualHost可以定义多个。 注意2:所有符合NameVirtualHost或标签定义的请求,都会被作为虚拟主机处理,而主服务器将不理会。NameVirtualHost定义了而标签没有定义的的请求,服务器会找不到相应的虚拟主机而将无法处理。所以每个NameVirtualHost定义的参数至少要有一个相匹配。 注意3:如果设置NameVirtualHost 或为*:80的话,所有针对80端口的请求,都会被虚拟主机处理,请求会根据域名指向某个虚拟主机。如果有来自80端口的请求,而所请求的域名没有被配置为虚拟主机,那将指向第一个虚拟主机。这样主服务器将无法收到来自80端口的任何请求。为此也要为主服务器配置一个虚拟主机。

ServerAdmin 管理员邮箱

DocumentRoot 网站目录 (注意:如果网站目录中的路径有空格,请在路径两端加上双引号)

ServerName 要绑定的域名(必填)

ServerAlias 要绑定的虚拟主机的别名。(可选,如果多个域名,中间以空格分隔,如果没有,则去掉该行)

支持*,?两种通配符,比如 *.abc.com,表示任意一个abc.com的二级域名都可访问。

CustomLog 用户日志文件(可选,如果不需要,则去掉该行)

ErrorLog 错误日志(可选,如果不需要,则去掉该行)

基于IP地址的虚拟主机

<VirtualHost 172.20.30.40>  
  DocumentRoot /www/example1  
  ServerName www.example.com  
</VirtualHost>  
Copy after login

<VirtualHost 172.20.30.50 192.168.0.10:80>  
  DocumentRoot /www/example2  
  ServerName www.example.org  
</VirtualHost>  
Copy after login

每个虚拟主机可定义多个IP,之间用空格隔开

各种虚拟主机的混用

Listen 80  
Listen 81  
  
NameVirtualHost 172.20.30.40  
  
<VirtualHost 172.20.30.40>  
    DocumentRoot /www/example1  
    ServerName www.example.com  
</VirtualHost>  
  
<VirtualHost 172.20.30.40>  
    DocumentRoot /www/example2  
    ServerName www.example.org  
</VirtualHost>  
  
NameVirtualHost 172.20.30.40:81  
  
<VirtualHost 172.20.30.40:81>  
    DocumentRoot /www/example3  
    ServerName www.example3.net  
</VirtualHost>  
  
# IP-based  
<VirtualHost 172.20.30.50>  
    DocumentRoot /www/example4  
    ServerName www.example4.edu  
</VirtualHost>  
  
<VirtualHost 172.20.30.60:81 172.20.30.40:81>  
    DocumentRoot /www/example5  
    ServerName www.example5.gov  
</VirtualHost>  
Copy after login

虚拟主机混用时的问题

一、虚拟主机混用可以这样理解:一行NameVirtualHost指令定义的所有虚拟主机为一组;该组与一个基于IP的虚拟主机平级。即把一行NameVirtualHost定义的整个组看作是一个基于IP的虚拟主机。 二、虚拟主机指定的端口必须是Listen定义的。如果虚拟主机没有指定端口,则认为是80端口。如果NameVirtualHost * 这样定义,是指所有地址的所有已定义端口。 三、更具体的地址定义优先。比如NameVirtualHost指令定义了*:80,而某个基于IP的虚拟主机定义为192.168.0.1:80,那么此时如有对192.168.0.1:80的请求,那请求会被优先指向192.168.0.1:80定义的虚拟主机。所以为了避免混乱,不要定义相互有交叉或包含的地址区间。 四、一个虚拟主机,可以同时为基于域名和基于IP的。如上一例中最后一个虚拟主机。这样符合两种定义的请求都会被指同一个虚拟主机。有时要区别内外网对虚拟主机的访问时可以这样,因为来自内网的请求可能和来自外网的请求可能不一样,但是它们需要指向同一个虚拟主机。

使用"_default_"虚拟主机

这个虚拟主机可以理解成基于IP的虚拟主机

<VirtualHost _default_:*>  
    DocumentRoot /www/default  
</VirtualHost>  
Copy after login

这个虚拟主机将接管与其它虚拟主机IP和端口不匹配的请求。不过如此一来,主服务器将不会处理任何请求。因此把主服务器配置成一个虚拟主机是必要的。

本地机器代理在其它机器上运行的虚拟主机

<VirtualHost 158.29.33.248>  
    ProxyPreserveHost On  
    ProxyPass /foo/no !  
    ProxyPass /foo http://192.168.111.2  
    ProxyPassReverse /foo http://192.168.111.2  
    ServerName hostname.example.com  
</VirtualHost>
Copy after login

一、首先这是一个基于IP的虚拟主机,它接收并处理对IP地址158.29.33.248的请求。 二、ProxyPass /foo http://192.168.111.2 将把对http://158.29.33.248/foo的请求转换为一个代理请求,该请求指向http://192.168.111.2。 三、ProxyPass /foo/no ! 不代理针对/foo/no的请求。这个必须放在正常代理指令之前。 四、ProxyPreserveHost On 意思是传送原始请求的Host信息给被代理的机器。 五、ProxyPassReverse /foo http://192.168.111.2 可以保证请求URL在其它机器上被重定向后,本机处理时也可以保持一致。具体看手册关于反向代理的部分。 六、基于域名的虚拟主机也是同样的道理。不管是什么类型的虚拟主机,它只是处理归它处理的请求而已。

配置实例

需求1:

这是一个基于IP的虚拟主机使用实例

一个项目数据应用,为了防止由于域名解析问题导致不能正常访问,因此采用IP的方式访问接口。比如:http://61.122.3.6:8080/ 这样的方式。虚拟主机的配置如下:

1、在httpd.conf中Listen 80下面增加一行,内容为:Listen 8080,即监听8080端口

2、配置虚拟主机配置

#NameVirtualHost 61.122.3.6:8080 这一行可以不需要。经测试,基于IP地址的虚拟主机可不用设置NameVirtualHost项。  
<VirtualHost 61.122.3.6:8080>  
    ServerAdmin webmaster@dummy-host.www.phpStudy.net  
    DocumentRoot "d:/web/openj"  
</VirtualHost>  
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!