Apache Virtual Host

Source: Internet
Author: User

Apache has two types of virtual hosts: IP-based virtual hosts and domain-based virtual hosts.

For IP-based virtual hosts, the configuration is as follows:

Namevirtualhost 10.10.10.11: 80
Namevirtualhost 10.10.10.12: 80

# Virtualhost 1
<Virtualhost 10.10.11: 80>
DocumentRoot "/AAA /"
Servername domain1
</Virtualhost>

# Virtualhost 2
<Virtualhost 10.10.12: 80>
DocumentRoot "/BBB /"
Servername domain2
</Virtualhost>

Note: because each IP address is configured with a virtual host, you only need to determine which virtual host to access based on the IP address. If you use domain2 for access, and set domain2 in hosts to point to 10.10.10.11, which virtual host will be accessed? Of course, the access to virtualhost 1 is determined based on the IP address, so servername is not very useful. The master said it can be used as an environment variable (that is, servername will appear in the environment variable ); you may ask, if you use 127.0.0.1 for access, you do not specify which virtual host to use for this IP address. In this case, the first virtual host (virtualhost 1) it is the default virtual host, that is, the default virtual host is used if there is no matching (matching with IP address and domain name, IP priority)

 

For domain name-based virtual hosts, the configuration is as follows:

# All IP addresses use virtual hosts
Namevirtualhost *: 80

# Virtualhost 1
<Virtualhost *: 80>
DocumentRoot "/AAA /"
Servername domain1
</Virtualhost>

# Virtualhost 2
<Virtualhost *: 80>
DocumentRoot "/BBB /"
Servername domain2
</Virtualhost>

Note: In this case, no matter which IP address you use (because it cannot be distinguished by IP address), you can access virtualhost 1 with domain1, and access virtualhost 2 with domain2;

For both IP-based and domain-based virtual hosts, the configuration is as follows:

# All IP addresses use virtual hosts
Namevirtualhost *: 80

# Virtualhost 1
<Virtualhost 10.10.11: 80>
DocumentRoot "/AAA /"
Servername domain1
</Virtualhost>

# Virtualhost 2
<Virtualhost 10.10.12: 80>
DocumentRoot "/BBB /"
Servername domain2
</Virtualhost>

# Virtualhost 3
<Virtualhost domain3: 80>
DocumentRoot "/CCC /"
Servername domain3
</Virtualhost>

Note:
1. note the IP address that domain3 points to when starting Apache, because domain3 in <virtualhost domain3: 80> must be interpreted as an IP address, during the analysis, we will treat it as the resolved IP address.

2. assume that domain3 is interpreted as 10.10.10.11. If the client uses domain3 to point to 10.10.10.11 for access, we find that there are two virtual hosts 10.10.11: 80, which are determined based on the servername, it is found that # virtualhost 3 is the most matched virtual host, so # virtualhost 3 is the virtual host to be accessed;

3. assume that domain3 is interpreted as 10.10.10.11. If the client uses domain3 to point to 10.10.10.12 for access, the client determines Based on the IP address that only # virtualhost 2 matches, so # virtualhost 2 is the virtual host to be accessed;

4. if you want to use domain3 to access the/CCC/directory, you must configure the servername as the virtual host of domain3 for both IP addresses. Do you want to change # virtualhost 3 to the following:
# Virtualhost 3
<Virtualhost *: 80>
DocumentRoot "/CCC /"
Servername domain3
</Virtualhost>
The answer is: no, because when the IP address is matched, a more precise match is preferred. Obviously, compared with the other two virtual hosts, access through any IP address, * It is not as accurate as a virtual host defined by an IP address. Therefore, # virtualhost 3 can only be accessed through the loopback address, use any host name to access # virtualhost 3;

Use of namevirtualhost:
If only one virtual host is set on an IP: port, the namevirtualhost IP: port can be left empty, because access from this IP: Port does not require any selection;
If more than one VM is set on an IP: Port, you must use the command namevirtualhost IP: port. Otherwise, access from this IP: port only accesses the first IP: port VM (because of its priority, it will be prompted during Apache startup );
If the command namevirtualhost IP: port is used, but no virtual host is defined for this IP: port, it seems that there will be no access to the wrong VM (because no one knows what you want, but Apache will still prompt you: [warn] namevirtualhost IP: Port has no virtualhosts );
If the virtual host specified by virtualhost does not match a namevirtualhost command, the virtual host will not be used (unless it is the first virtual host), which is equivalent to no namevirtualhost command,That is, if you have multiple virtual hosts, you must have the namevirtualhost command.

Note 1: namevirtualhost specifies the IP address or domain name used by the VM, but preferably the IP address. When using a domain name-based Vm, namevirtualhost is a required command. Multiple namevirtualhost can be defined.
NOTE 2: All requests that comply with the namevirtualhost or <virtualhost> label will be processed as virtual hosts, and the master server will ignore them. If the <virtualhost> label does not define a request, the server cannot find the corresponding virtual host and cannot process it. Therefore, each parameter defined by namevirtualhost must have at least one <virtualhost> matched.
NOTE 3: If namevirtualhost or <virtualhost> is set to *: 80, all requests for port 80 will be processed by the VM, and the requests will be directed to a VM based on the domain name. If there is a request from port 80 and the requested domain name is not configured as a VM, it will point to the first VM. In this way, the master server will not be able to receive any requests from port 80. Therefore, you must configure a virtual host for the master server.

 

Note:

Many commands can be used by default in the VM. I used to only record logs for access by different domain names. In fact, they access the same thing, so the DocumentRoot command can be omitted, I only wrote three commands, for example:

<Virtualhost *: 80>
Servername *
Transferlog "| rotatelogs/backup/apachelog/% Y % m/access_log. % Y % m % d 86400 480"
Errorlog "|/rotatelogs/backup/apachelog/% Y % m/error_log. % Y % m % d 86400 480"
</Virtualhost>

Mixed Use of VM:
I. A mix of virtual hosts can be understood as follows: all virtual hosts defined by a line of namevirtualhost commands are a group, which is at the same level as an IP-based virtual host. That is, the entire group defined by one namevirtualhost row is considered as an IP-based virtual host.
2. The port specified by the VM must be defined by listen. If no port is specified for the VM, port 80 is used. If namevirtualhost * is defined in this way, all ports of all addresses are defined.
3. More specific address definitions are preferred. For example, the namevirtualhost command defines *: 80, and an IP-based virtual host is defined as 192.168.0.1: 80. In this case, if there is a request for 192.168.0.1: 80, the request is directed to the VM defined at 192.168.0.1: 80. To avoid confusion, do not define overlapping or contained address ranges.
4. A virtual host can be both domain-based and IP-based. The last VM in the preceding example. In this way, requests that comply with the two definitions will be directed to the same virtual host. Sometimes you need to distinguish between Intranet and Internet access to virtual hosts, because requests from the Intranet may be different from those from the Internet, but they need to point to the same virtual host.

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.