Implementation of virtual domain names in Linux

Source: Internet
Author: User
Implementation of virtual domain names in Linux-Linux Enterprise Application-Linux server application information. The following is a detailed description. This article is for reference only

Outline

At present, the Linux operating system is very popular, so many friends want to know about the implementation process of virtual domain names in Linux. Next we will discuss it based on the popular RedHat. For more information about the principles of virtual domain names, see implementing virtual domain names under NT.
Directory

DNS server settings
Apache server configuration
CGI program Compilation
DNS server settings
Author: Zheng Tao

We know that the Internet is based on the TCP/IP protocol, and the IP address of the other party must be obtained for communication. This is achieved through the DNS server. Therefore, to implement a virtual domain name, the DNS server should first accept the virtual domain name, that is, map it to the specified IP address. Because we rely on Web servers to differentiate domain names, this IP address should naturally be managed by Web servers.
The bind dns server is bound to the RedHat6.0 operating system. Its Domain Name configuration file is "/etc/named. conf". Generally, the domain configuration file is placed under the "/var/named" directory.

Example 1. Configure the named. conf file:

Zone "***. com "{
Type master;
File "***. com ";
};
Zone "0.10.10.in-addr. arpa "{
Type master;
File "10.10.0 ";
};

This example describes "***. the domain configuration file of com is "/var/named /***. com ", the configuration file for the reverse domain is"/var/named/10.10.0 ". * ** The. com file maps the DNS domain name to an IP address.

Example 2. ***. com file Configuration:

@ In soa dns. ***. com. hostmaster. dns. ***. com .(
1998111003; serial
3600; refresh
900; retry
1209600; expire
43200; default_ttl
)
@ In mx 10 dns. ***. com.
@ In ns dns. ***. com.
@ In a 10.10.0.1
Www in a 10.10.0.1

Assume that the domain name to be added is aaa. ***. com and you want to refer to www. ***. com. An alias record should be added to the DNS Service and can be written as follows:
Aaa in cname www. ****. com.
If you need to configure a large number of virtual domain names, the ***. com file is very large and troublesome. We can use the symbol "*" to add it to the ***. com file:
* In cname www. ***. com.
In this way, it takes *** for all the settings not provided ***. all records ending with com are forwarded to www. ***. com, No matter aaa. ***. com or bbb. ***. com. This does not affect existing records. After configuring the DNS server, restart the daemon named:
[Root @ ***/root] #/etc/rc. d/init. d/named restart

Apache server is currently the most widely used Web server on the Internet. It can maintain very busy sites. RedHat 6.0 is bound with Apache Web Server 1.3. Its configuration file is located in the "/etc/httpd/conf" directory, including httpd. conf, srm. conf, and access. conf. The following describes the configurations related to virtual domain names:

1. Static Configuration
To modify the configuration file httpd. conf:
(1) first set UseCanonicalName to off. It indicates that the ServerName value is provided to the environment variable SERVER_NAME by replacing the server Host: header content.
(2) then add:
NameVirtualHost xxx. xxx is the IP address of the virtual server to be configured. Multiple virtual IP addresses can be configured here, which must be consistent with those configured on the DNS server. (Note: the IP address is used here, and the domain name is not used .)
(3) Add a record for each virtual domain name:

<Virtualhost xxx. xxx>
.....
</Virtualhost>

Xxx. xxx must be consistent with the IP address configured by NameVirtualHost. The following configuration parameters can be added between two flags:
ServerName is followed by the virtual domain name you want to add, such as aaa. ***. com;
Documentroot: add the path mapped to the local machine, for example, "/home/aaa ";
Redirect: If you map a URL to the remote end, it can be added to the backend. There are two parameters: the first is the relative path of the virtual domain name, and the second is the URL of the remote site;
After ServerAlias, you can add an alias for this domain name. Wildcards can be used, for example, *. aaa. **. com.

The following are two examples:

Example 3. Physical paths mapped to the local machine:

<Virtualhost xxx. xxx>
DocumentRoot "/home/test"
ServerName test. ***. com
<Virtualhost>

Example 4. URL mapped to the remote end:

<Virtualhost xxx. xxx>
ServerName test1.doamin.com
Redirect/http: // test. *** 1.com/welcome.htm
</Virtualhost>

There are also some parameters, such as the location of the log file, timeout settings, buffer settings, and so on. For details here, refer to the online help file of the Apache server. After the httpd. conf file is configured, restart the Apache background daemon httpd,
[Root @ ***/root] #/etc/rc. d/init. d/httpd restart

Each added virtual domain name requires a piece of configuration code located between <VirtualHost...>... </VirtualHost>, and the new configuration takes effect only after the httpd is restarted.

2. dynamic configuration

It can be found that if you want to configure a large number of virtual domain names, the length of the configuration file will be greatly increased, which will also slow Apache startup, occupy more memory, and it is not easy to implement online application. You can select Dynamic Mode for configuration. In this way, you do not have to write the configuration in advance, but write dynamic rules to automatically generate or read information from an independent configuration file at any time.

Apache has a powerful extension function, namely the module feature. The module can expand the functions of the server, which is loaded into the server only during running and use. This saves memory space and is faster than calling an external CGI program.

Now we use a powerful module to dynamically configure virtual domain names, which is mod_rewrite. It is responsible for listening to the URL address sent from the client and rewriting the URL based on a set of rule expressions. This is similar to the concept of URL alias, but it goes further. The output URL can be mapped to any URL address of other hosts. To modify the configuration file httpd. conf:
(1) set UseCanonicalName to off first.
(2) Use RewriteEngine on to open the rewrite engine. Use RewriteMap to set the location and properties of the configuration file, and use RewriteCond and RewriteRule to create corresponding rules.

Example 5: An Example of the configuration in the httpd. conf file:

RewriteEngine on
RewriteMap lowercase int: tolower
# Define the location of an independent configuration file
RewriteMap vhost dbm:/www/conf/vhostdbm
# Using an independent configuration file to remap virtual domain names
RewriteCond $ {vhost: % 1} ^ (/. *) $
RewriteRule ^/(. *) $ % 1/docs/$1

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

For the vhostdbm file format, see "CGI program compilation ".

To apply for a virtual domain name online, you must write the corresponding CGI program to dynamically modify the independent configuration file (the above vhostdbm file) and manage users (including user application, login, password Change ). This section only describes operations on independent configuration files. For more information about how to write CGI programs in Perl, see.

The vhostdbm file uses the dbm format to record data. Compared with common text files, the file search speed is faster and easier to modify. Many UNIX systems have a standard library called database management. This database stores a set of key-value pairs in a pair of disk files. It provides a simple database management tool to conveniently change, add, or delete data content.

Perl accesses dbm by connecting the associated array to the dbm database through a process similar to opening a file. When a new element is created in the array, the dbm database is changed immediately. Deleting an element also deletes the values in the dbm database. Available:

1. to associate a dbm database with a dbm array:
Dbmopen (% arrayname, "dbmfilename", $ mode );

If dbmfilename does not exist, create the database. The % arrayname parameter is an associated array of Perl (if the array already has values, these values will be deleted ). The joined array is connected to the dbm database called dbmfilename. $ Mode is the number used to control the permission of the database file when a database needs to be created. This number is specified as an octal value, and 0644 is often used, read-Only permissions are granted to new users except the host. The host has all permissions.

2. Close the dbm database:
Dbmclose (% arrayname );
% Arrayname is the array name associated with the dbm database.

Example 6. Open vhostdbm with VHOST or create a new dbm database:
Dbmopen (% VHOST, "vhostdbm", 0644 );

Example 7. Create a record or change an existing record (set the parameter names from the html file form to vhost and rhost ):
$ VHOST {$ FORM {'vhost' }}=$ FORM {'rhost '};

Example 8. delete an existing record (set the parameter name from the html file form to vhost ):
Delete $ VHOST {$ FORM {'vhost '}};

Example 9. Disable vhostdbm:
Dbmclose (% VHOST );

Note: If the input parameters have been verified, there are no duplicate records. Otherwise, the existing records may be confused.
Related Article

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.