First, change httpd.conf
Open the Appserv installation folder, locate the httpd.conf file, and then remove the # number in front of the following two lines of text, respectively.
#LoadModule Vhost_alias_module modules/mod_vhost_alias.so
Remove # means enable Apache's virtual host feature.
#Include conf/extra/httpd-vhosts.conf
To get rid of this line, it means to import the virtual host configuration from the conf/extra/httpd-vhosts.conf file.
Second, change httpd-vhosts.conf
Open the file and see something like the following. The configuration of the virtual host is also included in the following configuration. This content is explained below
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 virtual host, but preferably an IP address. When using a domain-based virtual host, Namevirtualhost is the necessary instruction. Namevirtualhost can define multiple.
NOTE 2: All requests that conform to the definition of namevirtualhost or <VirtualHost> tags are processed as virtual hosts, and the primary server is ignored. Namevirtualhost defines a request that is not defined by the <VirtualHost> tag, and the server cannot find the corresponding virtual host and will not be able to process it. So each of the namevirtualhost defined parameters must have at least one <VirtualHost> match.
Note 3: Assuming that the settings namevirtualhost or <VirtualHost> for *:80, all requests for 80port will be processed by the virtual host, the request according to the domain name point to a virtual host. Suppose there is a request from 80port, and the requested domain name is not configured as a virtual host, it will point to the first virtual host. This way, the primary server will not be able to receive any requests from 80port. To do this, you also configure a virtual host for the primary server.
ServerAdmin Admin Mailbox
DocumentRoot site folder (Note: If the path in the site folder has spaces, add a double argument to both ends of the path)
ServerName the domain name to bind to (required)
Serveralias the alias of the virtual host to bind. (optional, assuming multiple domain names, separated by a space, assuming no, then remove the line)
Support *,? Two wildcards, such as *.abc.com, indicate that you can access a abc.com two-level domain.
Customlog user log file (optional, if not required, remove the line)
Errorlog error log (optional, if not required, remove the line)
IP address-based virtual host
<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 virtual host can define multiple IPs, separated by a space
Hybrid 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>
Problems when a virtual host is mixed
One, the virtual host mix can understand this: a line of namevirtualhost directives defined by a group of all virtual hosts, the group and an IP-based virtual host peer. That is, the entire group defined by a row of Namevirtualhost as an IP-based virtual host.
Second, the port specified by the virtual host must be listen defined. Assuming that the virtual host does not specify port, it is 80port. Assuming that namevirtualhost * is defined, it means all defined ports for all addresses.
Third, more detailed address definition is preferred. For example, if the Namevirtualhost directive defines *:80, and an IP-based virtual host is defined as 192.168.0.1:80, then if there is a request for 192.168.0.1:80, then the request will be prioritized 192.168.0.1 : 80 defines the virtual host. Therefore, in order to avoid confusion, do not define the intersection of each other or include the address range.
四、一个 virtual hosts can be domain-based and IP-based at the same time. As the last virtual host in the previous example. Requests that conform to both definitions will be referred to the same virtual host. Sometimes it is necessary to make a difference between the intranet and the virtual host, because requests from the network may not be the same as requests from the extranet, but they need to point to the same virtual host.
Using the "_default_" virtual host
This virtual host can be understood as an IP-based virtual host
<virtualhost _default_:*> documentroot/www/default</virtualhost>
This virtual host will take over requests that do not match the IP and port of other virtual hosts. Only in this way, the primary server will not process any requests whatsoever. Therefore, it is necessary to configure the primary server as a virtual host.
A virtual host that the local machine agent executes on another machine
<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>
First, this is an IP-based virtual host that receives and processes requests for IP address 158.29.33.248.
Second, Proxypass/foo http://192.168.111.2 will convert the request to Http://158.29.33.248/foo to a proxy request, which points to http://192.168.111.2.
Three, proxypass/foo/no! The request for/foo/no is not proxied. This must be placed before the normal proxy instructions.
Proxypreservehost on means transmitting the host information of the original request to the machine being proxied.
Five, Proxypassreverse/foo http://192.168.111.2 can ensure that the request URL is redirected on other machines, the native processing can also be consistent. See the manual for the reverse proxy section in detail.
Six, the domain-based virtual host is the same reason. No matter what type of virtual host it is, it simply handles requests that are processed by it.
Configuration instance
Requirement 1:
This is an IP-based virtual host usage instance
A project data application, in order to prevent the domain name resolution problem caused by the inability to access the normal, so the use of IP access to the interface. Analogy: http://61.122.3.6:8080/this way. The configuration of the virtual host is as follows:
1, in httpd.conf Listen 80 Add the following line, the content is: Listen 8080, that is, monitoring 8080port
2. Configure the Virtual host configuration
#NameVirtualHost 61.122.3.6:8080 This line is not required. After testing, the virtual host based on IP address can not set the Namevirtualhost item. <virtualhost 61.122.3.6:8080> ServerAdmin [email protected] documentroot "D:/web/openj" </ Virtualhost>
apache2.2 Virtual Host Configuration