Apache2.2 Virtual Host Configuration

Source: Internet
Author: User
1. Change httpd. conf

Open the installation folder 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. Change httpd-vhosts.conf

Open the file and see the following similar content. The configuration of the VM is as follows. Next, describe the content.

NameVirtualHost *:80<VirtualHost *:80>    ServerAdmin [email protected]    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>
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 are processed as virtual hosts, and the master server ignores them. If namevirtualhost defines a request with the <virtualhost> label undefined, the server cannot find the corresponding virtual host and cannot process it. Therefore, each namevirtualhost must have at least one <virtualhost> matched Number of partitions.
NOTE 3: If namevirtualhost or <virtualhost> is set to *: 80, all requests for 80 ports will be processed by the VM, and the requests will be directed to a VM based on the domain name. Assume that there is a request from 80 port, and the requested domain name is not configured as a virtual host, it will point to the first virtual host. In this way, the master server will not receive any requests from 80 ports. Therefore, you must configure a virtual host for the master server.

Serveradmin administrator email

DocumentRoot site folder (Note: if there are spaces in the path in the site folder, add a double quotation mark 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. Assume that multiple domain names are separated by spaces. If there is no domain name, the row is removed)

*,? Two wildcard characters, for example, * .abc.com, indicate that a second-level domain name of abc.com can be used.

Customlog user log file (optional, if not required, remove this line)

Errorlog Error Log (optional, if not required, remove the row)


IP address-based VM

<VirtualHost 172.20.30.40>  DocumentRoot /www/example1  ServerName www.example.com</VirtualHost>
<VirtualHost 172.20.30.50 192.168.0.10:80>  DocumentRoot /www/example2  ServerName www.example.org</VirtualHost>
Each VM can define multiple IP addresses separated by spaces.

Mixed use of various virtual hosts

Listen 80Listen 81NameVirtualHost 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>

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.
2. The port specified by the VM must be defined by listen. If no port is specified for the VM, the port is 80. Assume that namevirtualhost * is defined as the defined port of all addresses.
3. More detailed 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. 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 included address ranges.
4. A virtual host can be domain-based or IP-based at the same time. 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 it is necessary to make a difference between the Intranet and the Internet when asking the VM, because requests from the Intranet may be different from those from the Internet, but they need to point to the same VM.

Use "_ default _" VM

This VM can be understood as an IP-based VM.

<VirtualHost _default_:*>    DocumentRoot /www/default</VirtualHost>
This VM will take over requests that do not match other vm ip addresses and ports. However, the master server will not process any request. Therefore, it is necessary to configure the master server as a virtual host.

Virtual Host executed by the Local Machine proxy on other machines

<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>
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 manual Manual's reverse proxy section.
6. Domain Name-based virtual hosts are the same. No matter what type of VM is, it only processes the requests it processes.

Configure an instance

Requirement 1:

This isIP-based VM usageInstance

A project data application uses an IP address to issue an interface in order to prevent domain name resolution errors that may result in abnormal renewal. For example, http: // 61.122.3.6: 8080. The configuration of the VM is as follows:

1. Add below listen 80 in httpd. conf? A row with the following content: Listen 8080, that is, listening to the 8080 Port

2. Configure the virtual host

# Namevirtualhost 61.122.3.6: 8080 this line can be unnecessary. After the trial, 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.