Introduction to the Setup method of server virtual host under Apache

Source: Internet
Author: User
Tags http request implement lowercase versions domain name server client hosting advantage

The principle of WWW server virtual host

The WWW server virtual host refers to a WWW server that uses a physical machine to act as multiple host names. For example, by a machine to provide http://www.company1.com, http://www.company2.com and other WWW services, and browsing the WWW site users do not feel this way with the different machines to provide different services what is the difference.

The advantage of using the WWW virtual host is that some small web sites, by sharing the same physical machine with other sites, can reduce the operating costs of the system and reduce the difficulty of management. In addition, for individual users, can also use this virtual host to establish their own independent domain name of the WWW server, there are many companies in the country to provide this free service.

There are two ways of working with WWW virtual hosts:

1.1 Virtual host mode based on IP address

In this way, different host names are resolved to different IP addresses, and these IP addresses are set on the machine that provides the virtual host service. The server determines which virtual host services the user requests based on the destination IP address of the user's request, thus further processing.

Disadvantage: The virtual host based on the IP address needs to provide a virtual host service on the machine to set up multiple IP addresses, both waste the IP address, but also limits the number of virtual hosts a machine can hold. Therefore, this approach is used more and more sparingly. However, this approach is the only virtual host method supported by the HTTP 1.0 protocol that was used earlier.

1.2 Virtual host mode based on host name

The HTTP 1.1 protocol adds support for host-based virtual hosts because of the drawbacks of the virtual host approach based on IP addresses. Specifically, when a client program makes a request to the WWW server, the host name that the customer wants to access is also passed to the WWW server through the "host:" statement in the request header. For example, www.company1.com, www.company2.com all correspond to the same IP address (that is, the same machine to provide services to these two virtual domain names), the client program to access the http://www.company1.com/ When index.html, the request header that is issued contains the following contents:

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 request is a service, and then further processing.

Advantages: The provision of virtual host services on the machine as long as the setting of an IP address, theoretically can give countless multiple virtual domain names to provide services, less resources, management convenience. Currently, this is basically the way to provide virtual hosting services.

Disadvantage: Not available under earlier HTTP 1.0 versions. In fact, the browsers now in use basically support the host-based virtual host approach.

Two, the Apache server under the virtual host Setup technology

2.1 Apache WWW Server Introduction

The Apache www server is the most widely used WWW server software on the Internet today. With a flexible configuration, it can accomplish almost the functionality 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 the Apache www server

With this type of virtual host, you first set up a separate IP address for each virtual host on the server. These IP addresses can be completed by adding multiple network adapters or by setting up multiple IP addresses on a single network card. With multiple IP addresses, you can set up Apache in one of the following two ways:

2.2.1 Run a copy of Apache for each virtual host

In this way, each Apache program can be run by a separate user, so each virtual host does not affect each other. When setting up this virtual host, just set up a set of configuration files for each Apache, the only thing to be aware of is that you must use the "Listen" statement to force each Apache to receive a service request only on its own IP address.

Advantages: Each virtual host between the mutual interference, high security.

Disadvantage: Occupy system resources more.

2.2. More than 2 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 the execution of CGI programs, may bring some serious security problems. When you set up this virtual host, you can set up information similar to the following for each virtual host:

       
        
         
        <virtualhost www.company1.com>; Documentroot/home/company1 ... </VirrualHost>;
       
        

Advantage: Less system resources than the previous one.

Disadvantage: Low security, each virtual host still needs to occupy an IP address.

2.3 The Apache www server with simple name based virtual host settings

In this way, each virtual host shares the same Apache, so there is no high security when CGI programs run. When you set up this virtual host, you can set up information similar to the following for each virtual host

       
        
         
        Namevirtualhost 111.222.33.44; In this IP address to receive the services of the virtual host <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: As long as one IP address can provide a large number of virtual host services.

Disadvantage: poor security. The configuration files need to be changed to maintain these virtual hosts, and the Apache process needs to be restarted to work. Therefore, it is not suitable for large-scale virtual hosting services.

2.4 Large-Name virtual host settings under Apache www server

The so-called large-scale virtual host, is to be able to provide a very large number of virtual host services, such as to provide more than hundreds of thousands of domain names of virtual hosting services. If you use the method discussed above, it is difficult to complete. One way to implement this service is to take advantage of the powerful URL rewrite capabilities of Apache. Here is an example to illustrate this approach.

2.4.1 URL rewrite functionality in Apache

Apache 1.2 and later versions have URL rewriting (Rewrite) features. Simply put, the URL rewrite function is when Apache received the request, in the process of processing in accordance with the pre-set rules, the request to modify the URL. These rules are mainly composed of regular expressions. Overrides are dependent on the URL entered, various server-side environment variables, content in the HTTP request header, and time, even through external programs that use information from the database to help with the rewrite.

URL Rewrite is very powerful, through URL rewriting, you can make Apache complete the extremely complex functions. Of course, the URL rewrite feature itself is more complex, and the detailed description of the URL rewrite feature can refer to Apache's random documentation.

2.4.2 uses the URL rewrite feature in Apache to implement a large-scaled virtual host setting based on name

Suppose the machine www.home.com provide large-scale virtual hosting services, such as abcde.home.com, hijk.home.com, etc. Where the virtual host name must be a letter or a number and at least 3 of the length. In order to prevent a directory of too many files or subdirectories have a significant negative impact on performance, the user's directory by the name of the hierarchical classification, such as the virtual host abcde.home.com files are stored under the directory/MEMBER/AB/DE/ABCDE. where "AB", "de" is the "ABCDE" of the front two characters and the last two characters. When a user requests http://abcde.home.com, the system should return the file below/member/ab/de/abcde.

Here's what you need to do to complete this feature

1. The setting of the domain name server

If the IP address of www.home.com is 202.103.190.57, add a row in the Home.com data file of the domain name server DNS:

--------------------------------------------------------------------

* in A 202.103.190.57

--------------------------------------------------------------------

Thus, all *.home.com IP addresses are resolved to 202.103.190.57, and there is no need to set up individual virtual hosts separately.

2. Apache Settings

First Use the command "Httpd-l" to check whether the Mod_rewrite module has been compiled into Apache, and if not, recompile Apache.

Then add the following statement in the Apache configuration file httpd.conf (which begins with "#" as a comment):

-----------------------------------------------------------------------

#禁止使用机器的正式名

Usecanonicalname off

#使能重写功能

Rewriteengine on

Rewritemap lowercase Int:tolower

# for security reasons, no rewriting of CGI programs

Rewritecond%{request_uri}!^/cgi-bin/

# no rewrite for 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, add it to the requested file path, 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

-------------------------------------------------------------------------

Advantages: As long as an IP address can provide a large number of virtual host services, maintenance convenience.

Disadvantage: Low security. The above example prohibits running the user's own CGI program to enhance security.

2.5 The redirected virtual host settings under the Apache www server

A redirected virtual host is a virtual host that provides only the redirection functionality without actually storing the user's home page data. For example, a site has multiple mirror servers, assumed to be www1.user.com, www2.user.com,...wwwn.user.com. These servers are placed on different networks, and customers from all over the world can access these servers at varying speeds. The site www.home.com provides virtual host services for these mirrored servers through a virtual domain name user.home.com. When the customer accesses the http://user.home.com, www.home.com redirects the customer to the site with the fastest access to the customer based on the user's IP address and the network topology information collected.

Redirecting the virtual Host service can also be accomplished by using the URL rewrite feature. Here's a simple rewrite rule:

-------------------------------------------------------------------------

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 produce complete HTTP reply header information. This program obtains the user's request URL through the environment variable path_info, obtains the user's IP address through the environment variable REMOTE_ADDR, and generates the redirect information according to the network topology situation, returns to the customer.

Reference documents:

1. Apache 1.3 User ' s Guide http://www.apache.org

Virtual Hosting Setup Using Apache

Abstract:

There are two types of WWW Virtual Hosting, ip-based and name-based. Those two types are introduced in-thesis, followed by some methods about WWW Virtual Hosting configuration under Apac The He and the difference between them are analyzed.



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.