TopApache virtual host documentation-dynamic configuration of a large number of Virtual Hosts

Source: Internet
Author: User
Tags reverse dns
TopApache VM documentation-dynamic configuration of a large number of VM instances-Linux Enterprise Application-Linux server application information. For details, see the following. Motivation

If your configuration file httpd. conf contains many And the content is roughly the same, you should be interested in the technology mentioned here. For example:

NameVirtualHost 111.22.33.44

ServerName www.customer-1.com
DocumentRoot/www/hosts/www.customer-1.com/docs
ScriptAlias/cgi-bin // www/hosts/www.customer-1.com/cgi-bin


ServerName www.customer-2.com
DocumentRoot/www/hosts/www.customer-2.com/docs
ScriptAlias/cgi-bin // www/hosts/www.customer-2.com/cgi-bin

# And so on...

ServerName www.customer-N.com
DocumentRoot/www/hosts/www.customer-N.com/docs
ScriptAlias/cgi-bin // www/hosts/www.customer-N.com/cgi-bin


The most basic idea is to use dynamic mechanisms to implement all these static Configuration section. This method has many advantages:

1. The configuration file becomes smaller so that Apache can be started faster and consume less memory.
2. To add a VM, you just need to create a proper directory in the file system and configure related DNS information without restarting Apache.

The main drawback is that you cannot use different log files for each virtual host. However, if different log files are recorded on Servers configured with a large number of virtual hosts, the maximum number of file descriptors allowed by the operating system is likely to be reached. A better way is to write logs to pipelines or first-in-first-out stacks, and enable other processes to sort the obtained log information (you can also make some historical statistics ).
Top
Overview

A virtual Host is defined in two parts: one is its IP address and the other is the HTTP "Host:" request header. The dynamic technology of a large number of virtual hosts is based on the idea of automatically inserting relevant information in the file path to be returned. Mod_vhost_alias can be easily implemented, but if your Apache version is earlier than 1.3.6, you must use mod_rewrite. Both are not enabled by default. to use them, they must be enabled in the configuration and compilation phases of Apache.

We need to do a lot of "camouflage" to make dynamic virtual hosts look like normal hosts. The most important thing is that Apache uses the Virtual Host Name (ServerName) to generate self-referential URL and other information. This is configured using the ServerName command and can be passed to the CGI script through the Environment Variable SERVER_NAME. The actual value used during running is controlled by the UseCanonicalName command. When UseCanonicalName is Off, the Virtual Host Name (ServerName) is taken from the "Host:" header in the request. When UseCanonicalName DNS is used, the host name is obtained through reverse DNS resolution of the virtual host's IP address. In the past, we used name-based Dynamic Virtual Hosts. Currently, IP address-based virtual hosts are commonly used. If Apache cannot determine the virtual Host name, it may be that the "Host:" header does not exist or DNS resolution fails. In this case, Apache uses the Host name entered When configuring ServerName.

Another thing that requires "Disguise" is the document root directory (configured by DocumentRoot and can be used by the DOCUMENT_ROOT environment variable as the CGI script ). In the general configuration mode, these settings are used by the core module to map URIs to the file system. However, if dynamic virtual host configuration is used, this information will be used by another module (mod_vhost_alias or mod_rewrite) that maps URIs to the file system in a different way than the core module. Neither of the two modules is responsible for setting the DOCUMENT_ROOT environment variable. Therefore, if the CGI or SSI program uses the DOCUMENT_ROOT environment variable, the error value is returned.
Top
Simple Dynamic VM

This is the configuration method in the httpd. conf file to achieve the same effect as the virtual host mentioned in the motivation section above, but the mod_vhost_alias module is used here:

# Obtain the Host name from the "Host:" Header
UseCanonicalName Off

# This log format can extract the host name from the first field
LogFormat "% V % h % l % u % t \" % r \ "% s % B" vcommon
CustomLog logs/access_log vcommon

# Include the host name in the file name path of the returned request
VirtualDocumentRoot/www/hosts/% 0/docs
VirtualScriptAlias/www/hosts/% 0/cgi-bin

Change UseCanonicalName Off to UseCanonicalName DNS to implement IP address-based virtual hosts. The server name to be inserted in the file path is obtained through the IP Address Resolution of the VM.
Top
A real personal homepage System

The preceding system can be used as the personal homepage server of the ISP. We used a slightly complex method to extract the substring from the host name (ServerName) and insert it into the file path. In this example, the document www.user.isp.com will be located in/home/user. Use a single cgi-bin directory for all virtual hosts.

# All preparations are the same as above, and then include the host name in the file path
VirtualDocumentRoot/www/hosts/% 2/docs

# Single cgi-bin directory
ScriptAlias/cgi-bin // www/std-cgi/

For more complex settings of VirtualDocumentRoot, see mod_vhost_alias.
Top
Sets up virtual systems for multiple hosts on the same server

For more complex settings, use Apache's Container to manage the scopes of various VM configurations. For example, you can use an IP address for personal homepage customers and use the following configuration for commercial customers. Naturally, the two use Together.

UseCanonicalName Off

LogFormat "% V % h % l % u % t \" % r \ "% s % B" vcommon


Options FollowSymLinks
AllowOverride All



Options FollowSymLinks
AllowOverride None



ServerName www.commercial.isp.com

CustomLog logs/access_log.commercial vcommon

VirtualDocumentRoot/www/commercial/% 0/docs
VirtualScriptAlias/www/commercial/% 0/cgi-bin



ServerName www.homepages.isp.com

CustomLog logs/access_log.homepages vcommon

VirtualDocumentRoot/www/homepages/% 0/docs
ScriptAlias/cgi-bin // www/std-cgi/

Top
More effective IP address-based VM

As mentioned in the first example, it is easy to change to an IP address-based virtual host setting. However, unfortunately, this approach is not efficient because DNS needs to be queried every time a request is processed. This problem can be avoided by adding IP addresses to the file system. In this way, you do not need to associate with the host name. IP addresses can also be used in log records to separate different logs. Apache will not perform DNS queries to determine the Host Name (ServerName.

# Obtain the host name from IP address reverse resolution
UseCanonicalName DNS

# Include IP addresses in logs to facilitate future sorting
LogFormat "% A % h % l % u % t \" % r \ "% s % B" vcommon
CustomLog logs/access_log vcommon

# Include IP addresses in the file path
VirtualDocumentRootIP/www/hosts/% 0/docs
VirtualScriptAliasIP/www/hosts/% 0/cgi-bin
Top
Use Apache of earlier versions

The above example is based on mod_vhost_alias, but it only appears after version 1.3.6. If your version is old, you can use mod_rewrite to achieve the same purpose, as shown below. However, it can only be a virtual Host based on the "Host:" header.

In addition, you must pay attention to log issues. Apache1.3.6 is the first version that supports "% V" log format commands. In version 1.3.0-1.3.3, the "% v" option is the same as "% V; in version 1.3.4, there are no equivalent commands. In all these versions, the command UseCanonicalName can appear in the. htaccess file, which means that the customer's settings may cause log disorder. Therefore, the best practice is to use the "% {Host} I" command, which can directly record the "Host:" header. Note that this may include ": port" at the end ", this is not the case when "% V" is used.
Top
Use mod_rewrite to implement a simple dynamic VM

The example here is taken from httpd. conf, which is equivalent to the case in the first example. The first half is roughly the same as the previous example, but it is modified to be backward compatible with mod_rewrite. The second half is configured with mod_rewrite for actual work.

Note: by default, mod_rewrite runs before all other URI conversion modules (such as mod_alias). Therefore, if you use these modules, mod_rewrite must be adjusted accordingly. At the same time, we also need to make some tricks for each dynamic virtual host, so that it is equivalent to ScriptAlias

# Obtain the Host name from the "Host:" Header
UseCanonicalName Off

# Sortable logs
LogFormat "% {Host} I % h % l % u % t \" % r \ "% s % B" vcommon
CustomLog logs/access_log vcommon


# ExecCGI is required here, because we cannot force CGI to be executed in the same way as ScriptAlias
Options FollowSymLinks ExecCGI


# Next is the key part

RewriteEngine On

# ServerName from the "Host:" header, which may be case-insensitive
RewriteMap lowercase int: tolower

# Process common documents first
# Enable the change of name/icons/function.
RewriteCond % {REQUEST_URI }! ^/Icons/
# CGI allowed
RewriteCond % {REQUEST_URI }! ^/Cgi-bin/
# Start "magic"
RewriteRule ^/(. *) $/www/hosts/$ {lowercase: % {SERVER_NAME}/docs/$1

# Process CGI now (we need to force a MIME type)
RewriteCond % {REQUEST_URI} ^/cgi-bin/
RewriteRule ^/(. *) $/www/hosts/$ {lowercase: % {SERVER_NAME}/cgi-bin/$1 [T = application/x-httpd-cgi]

# OK!
Top
Personal Homepage System Using mod_rewrite

The configuration here is the same as that in the second example.

RewriteEngine on

RewriteMap lowercase int: tolower

# Allows CGI to work
RewriteCond % {REQUEST_URI }! ^/Cgi-bin/

# Check whether the hostname is correct before RewriteRule can take effect
RewriteCond $ {lowercase: % {SERVER_NAME} ^ www \. [a-z-] + \. isp \. com $

# Connecting the VM name to the beginning of the URI
# [C] indicates that the result of this rewrite will be used in the next rewrite rule
RewriteRule ^ (. +) $ {lowercase: % {SERVER_NAME }}$ 1 [C]

# Create the actual file name
RewriteRule ^ www \. ([a-z-] +) \. isp \. com/(. *)/home/$1/$2

# Define the global CGI directory
ScriptAlias/cgi-bin // www/std-cgi/
Top
Use an independent VM configuration file

This layout utilizes the advanced features of mod_rewrite to convert data in an independent VM configuration file. This can be more flexible, but requires more complex settings.

The vhost. map file contains content similar to the following:

Www.customer-1.com/www/Mers MERs/1
Www.customer-2.com/www/Mers MERs/2
#...
Www.customer-N.com/www.mers MERs/N

Http. conf includes:

RewriteEngine on

RewriteMap lowercase int: tolower

# Define a ing File
RewriteMap vhost txt:/www/conf/vhost. map

# Process aliases like in the preceding example
RewriteCond % {REQUEST_URI }! ^/Icons/
RewriteCond % {REQUEST_URI }! ^/Cgi-bin/
RewriteCond $ {lowercase: % {SERVER_NAME} ^ (. +) $
# File-based re ing
RewriteCond $ {vhost: % 1} ^ (/. *) $
RewriteRule ^/(. *) $ % 1/docs/$1

RewriteCond % {REQUEST_URI} ^/cgi-bin/
RewriteCond $ {lowercase: % {SERVER_NAME} ^ (. +) $
RewriteCond $ {vhost: % 1} ^ (/. *) $
RewriteRule ^/(. *) $ % 1/cgi-bin/$1

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.