Apache Configuration name-based Virtual Host

Source: Internet
Author: User
Tags tld hosting

Original: http://httpd.apache.org/docs/2.2/vhosts/name-based.html

Environment WIN8 XAMP Integrated environment

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 need to has a separate IP address for each host. With name-based virtual hosting, the server relies the "the client to the" the hostname as part of the HTTP headers. Using this technique, many different hosts can share the same IP address.

To use name-based virtual hosting, must designate the IP address (and possibly port) on the server that would be ACC Epting 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 is used, you can use * as the argument to Namevirtu Alhost. If you ' re planning-multiple ports (e.g. running SSL) You should add a Port to the argument, such as *:80. Note that mentioning an IP address in a namevirtualhost 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 is associated with a network interface on the server.

The next step is to create a <VirtualHost> block for each different host, would like to serve. The argument to the <VirtualHost> directive must match a defined namevirtualhost directive. (In this usual case, this would be "*:80"). Inside each <VirtualHost> block, you'll need at minimum a ServerName directive to designate which host is served a nd a documentroot directive to show where in the filesystem the content for that host lives. Main host goes away

If you is adding virtual hosts to an existing Web server, you must also create a <VirtualHost> block for the Existi Ng host. The ServerName and documentroot included in this virtual host should is the same as the global ServerName and DocumentRoot . List This virtual host first in the configuration file so the it would act as the default host.

For example, suppose that is serving the domain Www.domain.tld and you wish to add the virtual host WWW.OTHERDOMAIN.T LD, which points at the same IP address. Then you simply add the following to httpd.conf:

Namevirtualhost *:80

<virtualhost *:80>
ServerName Www.domain.tld
Serveralias Domain.tld *.domain.tld
Documentroot/www/domain
</VirtualHost>

<virtualhost *:80>
ServerName Www.otherdomain.tld
Documentroot/www/otherdomain
</VirtualHost>

You can alternatively specify a explicit IP address in place of the the * in both the Namevirtualhost and <VirtualHost> directives. For example, the might want to do the order to run some name-based virtual hosts on one IP address, and either ip-base D, or another set of name-based virtual hosts on another address.

Many servers want to is accessible by more than one name. This was possible with the Serveralias directive, placed inside the <VirtualHost> section. For example in the first <VirtualHost> block above, the Serveralias directive indicates that the listed names is OT Her names which people can use to see this same Web site:

Serveralias Domain.tld *.domain.tld

Then requests for all hosts in the DOMAIN.TLD domain is served by the WWW.DOMAIN.TLD virtual host. The wildcard characters * and? Can is used to match names. Of course, you can ' t just do up names and place them in ServerName or Serveralias. You must first has your DNS server properly configured to map those names to a IP address associated with your server.

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

Finally, you can fine-tune the configuration of the virtual hosts by placing other directives inside the <virtualhos t> containers. Most directives can is placed in these containers and would 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 <VirtualHost> container) would be used only if t Hey is not overridden by the virtual host settings.

Now when a request arrives, the server would first check if it is using an IP address that matches the namevirtualhost. If It is, then it'll look at each <VirtualHost> section with a matching IP address and try to find one where the S Ervername or Serveralias matches the requested hostname. If it finds one, then it uses the configuration for that server. If No matching virtual host is found and then the first listed virtual host that matches the IP address would be used .

As a consequence, the first listed virtual host is the default virtual host. The documentroot from the main server would never be used if an IP address matches the namevirtualhost directive . If you would a-special configuration for requests that does not match any particular virtual host, simply put T Hat configuration in a <VirtualHost> container and list It first in the configuration file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.