Apache 虚拟主机配置 Vitrual Host

校睿宝培训机构ERP,学员课时管理软件
实验预约管理软件,实验室管理软件,大学、高校实验预约平台
网站日志分析软件,网站访问日志在线分析
哲涛服务器监控软件,IT运维管理软件,CPU、内存、磁盘监控软件
术语虚拟主机指的是在单一机器上运行多个网站 (例如 www.company1.com 和 www.company2.com) 。 虚拟主机可以“基于 IP”,即每个 IP 一个站点; 或者“基于名称”, 即每个 IP 多个站点。这些站点运行在同一物理服务器上的事实不会明显的透漏给最终用户。

Apache 是第一个支持基于 IP 的虚拟主机的服务器。 Apache 版本 1.1 和更新的版本同时支持基于 IP 和基于名称的虚拟主机。 基于名称的虚拟主机有时候称为基于主机非 IP 的虚拟主机.

以下是在 Apache 1.3 以及更新版本中,解释支持虚拟主机的所有详细信息的文档页面列表。

top

虚拟主机支持

  • 基于名称的虚拟主机 (每个 IP 多个站点)
  • 基于 IP 的虚拟主机 (每个 IP 一个站点)
  • 虚拟主机样例
  • 文件句柄限制 (或者日志文件太多)
  • 动态配置的大规模虚拟主机
  • 虚拟主机匹配的深入讨论
top

配置指令

  • NameVirtualHost
  • ServerName
  • ServerAlias
  • ServerPath

如果你要调试虚拟主机配置,你会发现 Apache 的命令行参数 -S 非常有用。即输入以下命令:

/usr/local/apache2/bin/httpd -S

这个命令将会显示 Apache 是如何解析配置文件的。仔细检查 IP 地址与服务器名称可能会帮助你发现配置错误 (参见 httpd 程序文档,以便了解其它命令行选项)。


Name-based Virtual Host Support


This document describes when and how to use name-based virtual hosts.

top

Name-based vs. IP-based Virtual Hosts

IP-based virtual hosts use the IP address of the connection to determine the correct virtual host to serve. Therefore you need to have a separate IP address for each host. With name-based virtual hosting, the server relies on the client to report the hostname as part of the HTTP headers. Using this technique, many different hosts can share the same IP address.

Name-based virtual hosting is usually simpler, since you need only configure your DNS server to map each hostname to the correct IP address and then configure the Apache HTTP Server to recognize the different hostnames. Name-based virtual hosting also eases the demand for scarce IP addresses. Therefore you should use name-based virtual hosting unless you are using equipment that explicitly demands IP-based hosting. Historical reasons for IP-based virtual hosting based on client support are no longer applicable to a general-purpose web server, unless you are using a mod_ssl version without SNI support (standard in Apache releases since 2.2.12).

top

Using Name-based Virtual Hosts

To use name-based virtual hosting, you must designate the IP address (and possibly port) on the server that will be accepting requests for the hosts. This is configured using the NameVirtualHost directive. In the normal case where any and all IP addresses on the server should be used, you can use * as the argument to NameVirtualHost. If you're planning to use multiple ports (e.g. running SSL) you should add a Port to the argument, such as *:80. Note that mentioning an IP address in aNameVirtualHost directive does not automatically make the server listen to that IP address. See Setting which addresses and ports Apache uses for more details. In addition, any IP address specified here must be associated with a network interface on the server.

The next step is to create a  block for each different host that you would like to serve. The argument to the  directive must match a defined NameVirtualHost directive. (In this usual case, this will be "*:80"). Inside each block, you will need at minimum a ServerName directive to designate which host is served and a DocumentRoot directive to show where in the filesystem the content for that host lives.

Main host goes away

If you are adding virtual hosts to an existing web server, you must also create a  block for the existing host. The ServerName and DocumentRoot included in this virtual host should be the same as the global ServerName andDocumentRoot. List this virtual host first in the configuration file so that it will act as the default host.

For example, suppose that you are serving the domain www.domain.tld and you wish to add the virtual host www.otherdomain.tld, which points at the same IP address. Then you simply add the following to httpd.conf:

NameVirtualHost *:80


ServerName www.domain.tld
ServerAlias domain.tld *.domain.tld
DocumentRoot /www/domain



ServerName www.otherdomain.tld
DocumentRoot /www/otherdomain

You can alternatively specify an explicit IP address in place of the * in both the NameVirtualHost and  directives. For example, you might want to do this in order to run some name-based virtual hosts on one IP address, and either IP-based, or another set of name-based virtual hosts on another address.

Many servers want to be accessible by more than one name. This is possible with the ServerAlias directive, placed inside the  section. For example in the first  block above, the ServerAlias directive indicates that the listed names are other names which people can use to see that same web site:

ServerAlias domain.tld *.domain.tld

then requests for all hosts in the domain.tld domain will be served by the www.domain.tld virtual host. The wildcard characters * and ? can be used to match names. Of course, you can't just make up names and place them in ServerName orServerAlias. You must first have your DNS server properly configured to map those names to an IP address associated with your server.

The complete list of names in the VirtualHost directive are treated just like a (non wildcard) ServerAlias.

Finally, you can fine-tune the configuration of the virtual hosts by placing other directives inside the  containers. Most directives can be placed in these containers and will then change the configuration only of the relevant virtual host. To find out if a particular directive is allowed, check the Context of the directive. Configuration directives set in the main server context (outside any  container) will be used only if they are not overridden by the virtual host settings.

Now when a request arrives, the server will first check if it is using an IP address that matches the NameVirtualHost. If it is, then it will look at each  section with a matching IP address and try to find one where the ServerName orServerAlias matches the requested hostname. If it finds one, then it uses the configuration for that server. If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.

As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a  container and list it first in the configuration file.

top

Compatibility with Older Browsers

As mentioned earlier, there are some clients who do not send the required data for the name-based virtual hosts to work properly. These clients will always be sent the pages from the first virtual host listed for that IP address (the primary name-based virtual host).

How much older?

Please note that when we say older, we really do mean older. You are very unlikely to encounter one of these browsers in use today. All current versions of any browser send the Host header as required for name-based virtual hosts.

There is a possible workaround with the ServerPath directive, albeit a slightly cumbersome one:

Example configuration:

NameVirtualHost 111.22.33.44


ServerName www.domain.tld
ServerPath /domain
DocumentRoot /web/domain

What does this mean? It means that a request for any URI beginning with "/domain" will be served from the virtual host www.domain.tld. This means that the pages can be accessed as http://www.domain.tld/domain/ for all clients, although clients sending a Host: header can also access it as http://www.domain.tld/.

In order to make this work, put a link on your primary virtual host's page to http://www.domain.tld/domain/. Then, in the virtual host's pages, be sure to use either purely relative links (e.g., "file.html" or "../icons/image.gif") or links containing the prefacing /domain/ (e.g., "http://www.domain.tld/domain/misc/file.html" or "/domain/misc/file.html").

This requires a bit of discipline, but adherence to these guidelines will, for the most part, ensure that your pages will work with all browsers, new and old.

校睿宝培训机构ERP,学员课时管理软件
实验预约管理软件,实验室管理软件,大学、高校实验预约平台
网站日志分析软件,网站访问日志在线分析
哲涛服务器监控软件,IT运维管理软件,CPU、内存、磁盘监控软件

上海哲涛网络科技有限公司版权所有 © 2005-2023       沪ICP备06058430号-1

沪公网安备 31011302000898号

点我咨询