Apache2.2 virtual host configuration

Source: Internet
Author: User
Tags to domain
: This article mainly introduces apache2.2 virtual host configuration. if you are interested in the PHP Tutorial, refer to it. 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.

[Plain] view plaincopy

  1. # LoadModule vhost_alias_module modules/mod_vhost_alias.so

Remove # it means to enable the virtual host function of apache.

[Plain] view plaincopy

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

[Plain] view plaincopy

  1. NameVirtualHost *: 80
  2. ServerAdmin webmaster@dummy-host.www.phpStudy.net
  3. DocumentRoot "C:/Program Files/Apache2/docs/dummy-host.www.phpStudy.net"
  4. ServerName dummy-host.www.phpStudy.net
  5. ServerAlias www.dummy-host.www.phpStudy.net
  6. ErrorLog "log/dummy-host.www.phpStudy.net-error.log"
  7. CustomLog "logs/dummy-host.www.phpStudy.net-access.log" common

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 Tag-defined requests are processed as virtual hosts, but the master server ignores them. NameVirtualHost defines If the request is not defined by the tag, the server cannot find the corresponding virtual host and cannot process it. Therefore, each NameVirtualHost must have at least one parameter. Match.
Note 3: If NameVirtualHost or If it is *: 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

[Plain] view plaincopy

  1. DocumentRoot/www/example1
  2. ServerName www.example.com

[Plain] view plaincopy

  1. DocumentRoot/www/example2
  2. ServerName www.example.org

Each VM can define multiple IP addresses separated by spaces.

Mixed use of various virtual hosts

[Plain] view plaincopy

  1. Listen 80
  2. Listen 81
  3. NameVirtualHost 172.20.30.40
  4. DocumentRoot/www/example1
  5. ServerName www.example.com
  6. DocumentRoot/www/example2
  7. ServerName www.example.org
  8. NameVirtualHost 172.20.30.40: 81
  9. DocumentRoot/www/example3
  10. ServerName www.example3.net
  11. # IP-based
  12. DocumentRoot/www/example4
  13. ServerName www.example4.edu
  14. DocumentRoot/www/example5
  15. ServerName www.example5.gov

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, 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.

Use "_ default _" VM

This virtual host can be understood as an IP-based virtual host.

[Plain] view plaincopy

  1. DocumentRoot/www/default

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

[Plain] view plaincopy

  1. ProxyPreserveHost On
  2. ProxyPass/foo/no!
  3. ProxyPass/foo http: // 192.168.111.2
  4. ProxyPassReverse/foo http: // 192.168.111.2
  5. ServerName hostname.example.com

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

[Plain] view plaincopy

  1. # 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.
  2. ServerAdmin webmaster@dummy-host.www.phpStudy.net
  3. DocumentRoot "d:/web/openj"

The above introduces the apache2.2 virtual host configuration, including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.