Apache2.2 virtual host configuration tutorial

Source: Internet
Author: User
Tags to domain

Apache2.2 virtual host configuration tutorial

1. Modify httpd. conf

Open the installation directory of appserv, find the httpd. conf file, and remove the # Before the following two lines of text.

# Loadmodule vhost_alias_module modules/mod_vhost_alias.so
Remove # It means to enable the virtual host function of Apache.

# Include CONF/extra/httpd-vhosts.conf
Remove this line # It means importing the VM configuration from the conf/extra/httpd-vhosts.conf File

2. Modify httpd-vhosts.conf
Open the file and see the following similar content. The configuration of the VM is also based on the following content. Next we will explain this content

Namevirtualhost *: 80

<Virtualhost *: 80>
Serveradmin [email protected]
DocumentRoot "C:/program files/apache2/docs/dummy-host.www.jbxue.com"
Servername dummy-host.www.jbxue.com
Serveralias www.dummy-host.www.jbxue.com
Errorlog "log/dummy-host.www.jbxue.com-error.log"
Customlog "logs/dummy-host.www.jbxue.com-access.log" common
</Virtualhost>
Namevirtualhost *: 80
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.
Serveradmin administrator email
DocumentRoot website directory (Note: if the path in the website directory contains spaces, add double quotation marks at both ends of the path)
Servername: The domain name to be bound (required)
The alias of the virtual host to bind to serveralias. (Optional. If multiple domain names are separated by spaces in the middle, if not, this line is removed)
*,? Two wildcard characters, such as * .abc.com, indicate that any second-level domain name of abc.com can be accessed.
Customlog user log file (optional. Remove this line if not required)
Errorlog Error Log (optional. Remove this line if not required)

IP address-based VM

<Virtualhost 172.20.30.40>
DocumentRoot/www/jbxue1
Servername www.jbxue.com
</Virtualhost>

<Virtualhost 172.20.30.50 192.168.0.10: 80>
DocumentRoot/www/jbxue2
Servername www.jbxue.org
</Virtualhost>
Each VM can define multiple IP addresses separated by spaces.
Mixed use of various virtual hosts

Listen 80
Listen 81

Namevirtualhost 172.20.30.40

<Virtualhost 172.20.30.40>
DocumentRoot/www/jbxue1
Servername www.jbxue.com
</Virtualhost>

<Virtualhost 172.20.30.40>
DocumentRoot/www/jbxue2
Servername www.jbxue.org
</Virtualhost>

Namevirtualhost 172.20.30.40: 81

<Virtualhost 172.20.30.40: 81>
DocumentRoot/www/jbxue3
Servername www.jbxue3.net
</Virtualhost>

# IP-based
<Virtualhost 172.20.30.50>
DocumentRoot/www/jbxue4
Servername www.jbxue4.edu
</Virtualhost>

<Virtualhost 172.20.30.60: 81 172.20.30.40: 81>
DocumentRoot/www/jbxue5
Servername www.jbxue5.gov
</Virtualhost>

Hybrid use of virtual hosts
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. Example of adding domain names and 301 redirection in Apache

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. Apache domain name-based Virtual Host Configuration

Use "_ default _" VM
This virtual host can be understood as an IP-based virtual host.

<Virtualhost _ default _: *>
DocumentRoot/www/Default
</Virtualhost>
This VM will take over requests that do not match the IP addresses and ports of other VM instances. However, the master server will not process any requests. Therefore, it is necessary to configure the master server as a virtual host.
Virtual Host on which the local machine proxy runs

<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.jbxue.com
</Virtualhost>
1. This is an IP-based virtual host that receives and processes requests to the IP address 158.29.33.248.
2. proxypass/Foo http: // 192.168.111.2 converts a request for http: // 158.29.33.248/Foo to a proxy request pointing to http: // 192.168.111.2.
3. proxypass/Foo/No! No proxy for/Foo/no requests. This must be placed before the normal PROXY command.
4. proxypreservehost on refers to transmitting the host information of the original request to the proxy machine.
5. proxypassreverse/Foo http: // 192.168.111.2 can ensure that the request URL can be consistent during local processing after being redirected on other machines. For details, refer to the manual on reverse proxy.
6. Domain Name-based virtual hosts are the same. No matter what type of VM, it only processes the requests it processes.

Configure an instance
Requirement 1:
This is an IP-based VM instance.
A project data application uses IP addresses to access interfaces to prevent normal access due to domain name resolution issues. For example, http: // 61.122.3.6: 8080. The configuration of the VM is as follows:
1. Add a line under listen 80 in httpd. conf with the following content: Listen 8080, that is, listening to port 8080
2. Configure the virtual host

# Namevirtualhost 61.122.3.6: 8080 is not required. After testing, you do not need to set the namevirtualhost item for the IP address-based virtual host.
<Virtualhost 61.122.3.6: 8080>
Serveradmin [email protected]
DocumentRoot "D:/web/openj"
</Virtualhost>

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.