I. Principles of WWW Server virtual host
A www Server VM is a WWW server that uses a physical machine to act as multiple host names. For example, by a machine at the same time to provide http://www.company1.com, http://www.company2.com and other WWW services, and browse these WWW site users do not feel this way with different machines to provide different services.
The advantage of using WWW virtual hosts is that some small-scale websites share the same physical machine with other websites, which can reduce system operation costs and reduce management difficulty. In addition, individual users can also use this virtual hosting method to create WWW Servers with their own independent domain names. Currently, many domestic companies provide this free service.
WWW Virtual hosts can work in two ways:
1.1 IP address-based VM mode
In this way, different host names are resolved to different IP addresses, and these IP addresses are set on the machines that provide the VM service. The server determines the virtual host service requested by the user based on the target IP address of the user request, so as to further process the service.
Disadvantages: the IP address-based virtual host method requires that multiple IP addresses be set up on the machine that provides the virtual host service, which wastes IP addresses and limits the number of virtual hosts that a machine can accommodate. Therefore, this method is rarely used. However, this is the only way to use HTTP 1.0 to support virtual hosts.
1.2 Host name-based VM
Because the IP address-based VM has the following disadvantages, the support for host name-based VM is added to HTTP 1.1. Specifically, when a client sends a request to the WWW server, the Host name that the client wants to access is also passed to the WWW server through the "Host:" statement in the request header. For example, www.company1.com and www.company2.com all correspond to the same IP address (that is, the same machine provides services for the two virtual domain names). When the customer program needs to access http://www.company1.com/index.html, the request header contains the following content:
GET/index.html HTTP/1.1
Host: www.company1.com
.....
After receiving the request, the WWW server program can check the "Host:" statement to determine the service of the virtual Host requested by the client program, and then further process the request.
Advantage: as long as one IP address is set on the machine that provides the VM service, theoretically it can provide services to countless virtual domain names, which consumes less resources and facilitates management. This method is basically used to provide the VM service.
Disadvantage: it cannot be used in earlier versions of HTTP 1.0. In fact, the current browser basically supports the host name-based virtual host mode.
II. Virtual host setting technology under Apache server
2.1 Apache WWW server overview
The Apache WWW server is currentlyInternetThe most widely used WWW server software. With flexible configuration, it can accomplish almost what you want. This article describes how to set up various virtual hosts in ApacheMethod.
2.2 IP address-based VM settings for Apache WWW Server
To use this virtual host method, you must first set an IP address for each virtual host on the server. These IP addresses can be completed by adding multiple NICs or setting up multiple IP addresses on one Nic. With multiple IP addresses, you can set Apache in either of the following ways:
2.2.1 run an Apache copy for each VM
In this way, each Apache program can be run by a single user, so each virtual host does not affect each other. When setting such a VM, you only need to set a configuration file for each Apache Copy. The only thing you need to note is that you must use the "Listen" statement, each copy of Apache is forced to receive service requests only on its own IP address.
Advantage: each virtual host does not interfere with each other, and the security is high.
Disadvantage: it occupies a large amount of system resources.
2.2.2 Multiple virtual hosts share the same Apache
In this way, each virtual host shares the same Apache, so there is a certain impact between each virtual host, especially when executing CGI programs, it may bring some serious security problems. When setting this type of virtual host, you only need to set the following information for each virtual host:
<VirtualHost www.company1.com>;
DocumentRoot/home/company1
...
</VirrualHost>;
Advantage: less system resources are occupied than the previous method.
Disadvantage: low security. Each virtual host still needs one IP address.
2.3 simple name-based VM settings on the Apache WWW Server
In this way, each virtual host shares the same Apache. Therefore, when a CGI program is running, the security is not high. When setting this type of virtual host, you only need to set the following information for each virtual host:
NameVirtualHost 111.222.33.44; receives services from the VM at this IP address.
<VirtualHost 111.222.33.44>;
ServerName www.company1.com
DocumentRoot/www/company1
</VirtualHost>;
<VirtualHost 111.222.33.44>;
ServerName www.company2.com
DocumentRoot/www/company2
</VirtualHost>;
Advantage: a single IP address can provide a large number of VM services.
Disadvantage: poor security. To maintain these virtual hosts, you must change the configuration file and restart the Apache process. Therefore, it is not suitable for large-scale VM services.
2.4 name-based large-scale VM settings under Apache WWW Server
A large-scale VM is a virtual host service that can provide a large number of VM services, such as a service that can provide more than 100,000 domain names. If you use the methods discussed above, it is very difficult to complete. One way to implement this service is to use the powerful URL rewriting feature of Apache. This method is described in the following example.
2.4.1 URL rewriting in Apache
Apache 1.2 and later versions have the URL rewriting (Rewrite) function. Simply put, the URL rewriting function is to modify the URL in the request according to the preset rules after Apache receives the request. These rules are mainly composed of regular expressions. Rewrite depends on the input URL, various server-side environment variables, content and time in the HTTP request header, and can even be used by external programs.DatabaseTo help rewrite.
URL rewriting is very powerful. Through URL rewriting, Apache can perform exceptionally complex functions. Of course, the URL rewriting function is also complicated. For details about the URL rewriting function, see The Apache Random document.
2.4.2 use the URL rewriting function in Apache to implement name-based large-scale VM settings
Assume that the machine www.home.com provides large-scale VM services, such as abcde.home.com and hijk.home.com. The virtual host name must be a letter or number with a length of at least 3. In order to prevent too many files or subdirectories under a directory from negatively affecting the performance, the user's directories are classified by name, for example, the files related to the virtual host abcde.home.com are stored in the/member/AB/de/abcde directory. "AB" and "de" are the first and last two characters of "abcde. When the user requests a http://abcde.home.com, the system should return the files under/member/AB/de/abcde. The following settings are required to complete this function:
1. Domain name server settings
Assume that the IP address of www.home.com is 202.103.190.57. Add a line to the home.com data file of the DNS server:
--------------------------------------------------------------------------------
* In a 202.103.190.57
--------------------------------------------------------------------------------
In this way, all * .home.com IP addresses will be resolved to 202.103.190.57, so there is no need to set each virtual host separately.
2. Apache settings
Run the "httpd-l" command to check whether the mod_rewrite module has been compiled into Apache. If not, recompile Apache.
Then add the following statement to the Apache configuration file httpd. conf (note the statements starting ):
--------------------------------------------------------------------------------
# Disable the official name of the machine
UseCanonicalName off
# Enable rewrite function
RewriteEngine on
RewriteMap lowercase int: tolower
# For security considerations, do not rewrite the CGI program
RewriteCond % {REQUEST_URI }! ^/Cgi-bin/
# Do not rewrite www.home.com or rewrite others
RewriteCond $ {lowercase: % {HTTP_HOST }}! ^ Www.home.com (. *) $
RewriteCond $ {lowercase: % {HTTP_HOST} ^ [a-z0-9-] + .home.com (. *) $
# First, change the machine name to lowercase, add it to the file path of the request, and continue processing.
RewriteRule ^ (. +) $ {lowercase: % {HTTP_HOST} $1 [C]
# Rewrite request
RewriteRule ^ ([a-z0-9]) ([a-z0-9]) ([a-z0-9]) .home.com ([.] *)/(. *)/member/$1 $2/$2 $3/$1 $2 $3/$5
RewriteRule ^ ([a-z0-9]) ([a-z0-9]) ([a-z0-9] *) ([a-z0-9]) ([a-z0-9]) .home.com ([.] *)/(. *)/member/$1 $2/$4 $5/$1 $2 $3 $4 $5/$7
---------------------------------------------------------------------------------
Advantage: a single IP address can provide a large number of VM services for easy maintenance.
Disadvantage: low security. In the preceding example, to enhance security, you are not allowed to run your own CGI program.
2.5 redirect VM settings under Apache WWW Server
A redirected VM is a VM that only provides the redirection function and does not actually store user homepage data. For example, a website has multiple backup storage servers, such as www1.user.com, www2.user.com ,... Wwwn.user.com. These servers are deployed on different networks, and the access speed of customers distributed across different regions to these servers is also different. The site www.home.com provides virtual host services for servers with these images through the virtual domain name user.home.com. When a customer accesses a http://user.home.com, www.home.com redirects the customer to the site with the fastest access speed for the customer based on the user's IP address and network topology information collected.
You can also use the URL rewriting function to redirect the VM service. Here we only provide simple rewrite rules:
Bytes ------------------------------------------------------------------------------------
RewriteRule ^ ([a-z0-9] +) .home.com ([.] *)/(. *)/www/cgi-bin/nph-redirect.cgi/$1/$3 [T = application/x-httpd-cgi, L]
Bytes ------------------------------------------------------------------------------------
Here, the nph-redirect.cgi is a CGI program, in order to achieve automatic redirection, it needs to generate the complete HTTP response header information. The program obtains the URL requested by the user through the environment variable PATH_INFO, obtains the user's IP address through the environment variable REMOTE_ADDR, generates redirection information based on the network topology, and returns it to the customer.