Virtual host settings under Apache

First, the principle of
WWW server virtual host WWW server virtual host refers to the use of a physical machine, acting as a WWW server with multiple host names. For example, a single machine provides WWW services such as http://www.company1.com, http://www.company2.com, etc., and users who browse these WWW sites cannot feel that they are served by different machines in this way. What is the difference between the different services provided.
The advantage of using WWW web hosting is that some small-scale websites can reduce the operating cost of the system and the difficulty of management by sharing the same physical machine with other websites. In addition, for individual users, this virtual host method can also be used to establish a WWW server with its own independent domain name. At present, many domestic companies provide this free service.
WWW virtual host has two working modes:
1.1 IP address-based virtual host mode In
this mode, different host names resolve to different IP addresses, and these IP addresses are also set on the machine that provides virtual host services. The server determines which virtual host service is requested by the user according to the destination IP address requested by the user, so as to perform further processing.
Disadvantages: The IP address-based virtual host method needs to set up multiple IP addresses on the machine that provides the virtual host service, which not only wastes the IP address, but also limits the number of virtual hosts that a machine can accommodate. So this method is less and less used. However, this method is the only virtual host method supported by the early HTTP 1.0 protocol.
1.2 Virtual host method based on host name
Since the IP address-based virtual host method has the above shortcomings, the HTTP 1.1 protocol adds support for the host name-based virtual host. Specifically, when the client program 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 both correspond to the same IP address (that is, the same machine provides services to these two virtual domain names), and the client program needs to access http://www.company1. com/index.html, the sent request header contains the following content:
GET /index.html HTTP/1.1
Host: www.company1.com
.....
After the WWW server program receives this request, it can check the "Host :" statement to determine which virtual host the client program requests is the service of, and then further processing.
Advantages: As long as one IP address is set on a machine that provides virtual host services, in theory, it can provide services for countless virtual domain names, occupying less resources and convenient management. At present, this method is basically used to provide virtual host services.
Disadvantage: Does not work with earlier HTTP 1.0 versions. In fact, the browsers in use now basically support the virtual host method based on the host name.
Second, the virtual host setting technology under the Apache server
2.1 Introduction to the
Apache WWW server Apache WWW server is currently the most widely used WWW server software on the Internet. With flexible configuration, it can do almost anything you want. This article mainly introduces the method of setting up various virtual hosts under Apache.
2.2 IP address-based virtual host settings under Apache WWW server
To use this virtual host method, first set an IP address for each virtual host on the server. These IP addresses can be accomplished by adding multiple network cards or setting up multiple IP addresses on one network card. Once you have multiple IP addresses, you can set up Apache in one of two ways:
2.2.1 Run a copy of Apache for each virtual host
In this way, each copy of Apache can be run as a separate user, so Each virtual host does not affect each other. When setting up this kind of virtual host, you only need to set a set of configuration files for each Apache. The only thing you need to pay attention to is: you must use the "Listen" statement to force each Apache to be only on the IP address that belongs to "own". Receive service requests.
Advantages: each virtual host does not interfere with each other, and the security is high.
Disadvantages: take up a lot 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 influence between each virtual host, especially when executing CGI programs, it may bring some serious security question. When setting up this kind of virtual host, you only need to set information similar to the following for each virtual host
<VirtualHost www.company1.com>
DocumentRoot /home/company1

</VirrualHost>
Advantage: It occupies less system resources than the previous method.
Disadvantages: low security, each virtual host still needs to occupy an IP address.
2.3 Simple name-based virtual host setup under Apache WWW server
In this way, each virtual host shares the same Apache, so when CGI programs are running, the security is not high. When setting up this kind of virtual host, you only need to set information similar to the following for each virtual host
    NameVirtualHost 111.222.33.44 ; Receive the services of the virtual host 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>
Advantages: Only one IP address can provide a large number of virtual host services.
Cons: Poor security. Maintenance of these virtual hosts requires changes to configuration files and requires a restart of the Apache process for this to work. Therefore, it is not suitable for large-scale web hosting services.
2.4 Name-based large-scale virtual host settings under the Apache WWW server The
so-called large-scale virtual host refers to the ability to provide a large number of virtual host services, such as the virtual host service that can provide more than hundreds of thousands of domain names. This is difficult to accomplish using the methods discussed above. One way to implement such a service is to take advantage of Apache's powerful URL rewriting capabilities. An example is given below to illustrate this approach.
2.4.1 URL rewriting function in Apache
Apache 1.2 and later versions have URL rewrite (Rewrite) function. Simply put, the URL rewriting function is that after Apache receives the request, it can modify the URL in the request according to pre-set rules during processing. These rules consist mainly of regular expressions. The rewrite depends on the input URL, various server-side environment variables, the content of the HTTP request header, the time, and even the information in the database can be used by an external program to help the rewrite.
The URL rewriting function is very powerful. Through URL rewriting, Apache can complete extremely complex functions. Of course, the URL rewriting function itself is more complicated. For the detailed description of the URL rewriting function, please refer to the random documentation of Apache.
2.4.2 Use the URL rewriting function in Apache to implement large-scale virtual host settings based on names
Suppose that machine www.home.com provides large-scale virtual host services with names such as abcde.home.com, hijk.home.com, etc. where the virtual hostname must be alphanumeric and at least 3 in length. In order to prevent too many files or subdirectories in a directory from having a great negative impact on performance, the user's directories are classified according to their names. For example, the relevant files of the virtual host abcde.home.com are stored in the directory /member/ab/ de/abcde below. Among them, "ab" and "de" are the first two characters and the last two characters of "abcde" respectively. When a user requests http://abcde.home.com, the system should return the file under /member/ab/de/abcde. The following are the settings required to complete this function:
1. Setting of the domain name server
Assuming that the IP address of www.home.com is 202.103.190.57, add a line to the home.com data file of the domain name server DNS:
* IN A 202.103.190.57
In this way, all the IP addresses of *.home.com will be resolved to 202.103.190.57, so there is no need to set it separately for each virtual host.
2. Apache settings
First use the command "httpd -l" 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 (the ones starting with "#" are comments): #Prohibit
the use of the official name of the machine
UseCanonicalName off #Enable
the rewrite function
RewriteEngine on
RewriteMap lowercase int:tolower
# For security Consider, do not rewrite CGI programs
RewriteCond %{REQUEST_URI} !^/cgi-bin/
# Do not rewrite www.home.com, other rewrite
RewriteCond ${lowercase:%{HTTP_HOST}} !^www\ .home\.com(\.*)$
RewriteCond ${lowercase:%{HTTP_HOST}} ^[a-z0-9-]+\.home\.com(\.*)$
# First change the machine name to Lowercase, prepend to the requested file path, and continue processing
RewriteRule ^(.+) ${lowercase:%{HTTP_HOST}}$1 [C]
# Rewrite the 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
Pros: Just one IP address Can provide a large number of virtual host services, easy maintenance.
Disadvantage: low security. In the above example, in order to enhance security, it is forbidden to run the user's own CGI program.
2.5 Redirection virtual host setting under Apache WWW server
Redirection virtual host refers to the virtual host that only provides redirection function, but does not actually store user homepage data. For example, a site has multiple mirror servers, assuming www1.user.com, www2.user.com, ... wwwn.user.com. These servers are placed on different networks, and the speed at which the distributed customers access these servers is also different from each other. The site www.home.com provides virtual hosting services for these mirrored servers through the virtual domain name user.home.com. When a customer visits http://user.home.com, www.home.com redirects the customer to the site with the fastest access speed for the customer according to the user's IP address and the collected network topology information.
Redirecting virtual hosting services can also be done by using the URL rewriting feature. Only simple rewrite rules are given here:
RewriteRule ^([a-z0-9]+)\.home\.com([\.]*)/(.*) /www/cgi-bin/nph-redirect .cgi/$1/$3 [T=application/x-httpd-cgi,L]
Among them, nph-redirect.cgi is a CGI program, in order to realize automatic redirection, it needs to generate 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, and generates redirection information according to the network topology and returns it to the client.
3. Summary
WWW server virtual host is used more and more, especially for small and medium-sized sites, by using virtual host technology, it can reduce overhead and reduce maintenance and management workload.
This paper introduces two working modes of WWW server virtual host, and discusses the setting technology of various virtual hosts under Apache WWW server, especially large-scale virtual hosts, and compares the characteristics of various methods.
When users have high security requirements, IP address-based virtual hosts can be used, and each virtual host is served by a separate Apache process.
In other cases, the virtual host service can be set according to actual requirements.

Follow the official account to get massive videos

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326081394&siteId=291194637