apache2.2 Virtual Host Configuration detailed

Source: Internet
Author: User

I. Modification of HTTPD.CONF

Open the Appserv installation directory, locate the httpd.conf file, and remove the # number in front of the following two lines, respectively.

1 #

Remove # means enable Apache's virtual host feature.

1 # 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.

Ii. Modification of httpd-vhosts.conf

Open the file and see something like the following. The configuration of the virtual host is also configured with reference to the following content. This content is explained below

  1  namevirtualhost *:80  2   3  <virtualhost *:80>  4  ServerAdmin [Email protected]net   5  documentroot "C:/Program files/apache2/docs/ Dummy-host.www.phpstudy.net " 6  ServerName Dummy-host.www.phpstudy.net   7  serveralias www.dummy-host.www.phpstudy.net   8  errorlog" Logs/dummy-host.www.phpstudy.net-error.log " 9  Customlog "Logs/dummy-host.www.phpstudy.net-access.log"  common " Span style= "color: #008080;" >10  </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 namevirtualhost defined parameter must have at least one <VirtualHost> match. Note 3: If you set Namevirtualhost or <VirtualHost> as *:80, all requests for 80 ports will be handled by the virtual host, and the request will be directed to a virtual host based on the domain name. If there is a request from port 80, 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 port 80. To do this, you also configure a virtual host for the primary server.

ServerAdmin Admin Mailbox

DocumentRoot Site Directory (note: If the path in the site directory has spaces, enclose the path with double quotes)

ServerName the domain name to bind to (required)

Serveralias the alias of the virtual host to bind. (optional, if multiple domain names are separated by a space, if not, the line is removed)

Support *,? Two wildcard characters, such as *.abc.com, indicate that any one of the abc.com's two-level domain names is accessible.

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
1 2   documentroot/www/3   ServerName www.example.  4

1 2   documentroot/www/3   ServerName www.example.  4

Each virtual host can define multiple IPs, separated by a space

Hybrid of various virtual hosts
1Listen 802Listen 813    4Namevirtualhost 172.20.30.405    6<virtualhost 172.20.30.40>7documentroot/www/example18ServerName Www.example.com9</VirtualHost>Ten     One<virtualhost 172.20.30.40> Adocumentroot/www/example2 -ServerName Www.example.org -</VirtualHost> the     -Namevirtualhost 172.20.30.40:81 -     -<virtualhost 172.20.30.40:81> +documentroot/www/Example3 -ServerName Www.example3.Net +</VirtualHost> A     at #ip-based -<virtualhost 172.20.30.50> -documentroot/www/example4 -ServerName www.example4.edu -</VirtualHost> -     in<virtualhost 172.20.30.60:81 172.20.30.40:81> -documentroot/www/Example5 toServerName Www.example5.Gov +</VirtualHost>
Problems when a virtual host is mixed

One, the virtual host mix can be understood as follows: a line of namevirtualhost directives defined by all the virtual host group, 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. If the virtual host does not have a specified port, it is considered to be port 80. If namevirtualhost * is defined, it refers to all defined ports for all addresses. Third, more specific 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 contain the range of addresses. 四、一个 virtual host, which can be both domain-based and IP-based. 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 differentiate the intranet from accessing the virtual host, because requests from the intranet 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

1 2     documentroot/www/default3

This virtual host will take over requests that do not match the IP and port of other virtual hosts. However, the primary server will not process any requests. Therefore, it is necessary to configure the master server as a virtual host.

A virtual host that the local machine agent runs on another machine

 1  <virtualhost 158.29.33.248> 2    Proxypreservehost on  3  Proxypass /foo/no! 4  proxypass/foo http:192.168.111.2  5  proxypassreverse/foo http:// 192.168.111.2  6  ServerName Hostname.example. com  7  </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 specifically. Six, the domain-based virtual host is the same truth. Regardless of the type of virtual host, it only 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 IP access to the interface. For example: http://61.122.3.6:8080/such a way. The configuration of the virtual host is as follows:

1, in httpd.conf Listen 80 add a line below, the content is: Listen 8080, that is, listening to 8080 port

2. Configure the Virtual host configuration

1 #  23     ServerAdmin [email protected]4     5

apache2.2 Virtual Host Configuration detailed

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.