The Apache virtual Host (vhost) configuration form in Linux

Source: Internet
Author: User
Tags domain name server tld subdomain

Run multiple domain-based Web sites on an IP address.
Your server has an IP address, and there are many mappings (cnames) to this machine in DNS. You and you want to run the www.example1.com and www.example2.org two sites on this machine.

Attention
Creating a virtual host in the configuration of your Apache server does not automatically update the host name in your DNS. You must add your own domain name in DNS to point to your IP address. Otherwise someone else is not able to see your Web site. You can add this entry to your Hosts file to test it, but this method only works with machines that have these entries.

Server Configuration

  code is as follows copy code

# ensure That is Apache listens on port
Listen

# Listen for virtual host requests to all IP addresses
Namevirtual Host *

<virtualhost *>
documentroot/www/example1
ServerName www.example1.com

# Other directives here

</virtualhost>

<virtualhost *>
Documentroot/www/example2
ServerName www.example2.org

# Other directives here

</virtualhost>

Because the asterisk matches all the addresses, the primary server does not receive any requests. Because Www.example1.com first appears in the configuration file, it has the highest priority and can be considered the default or primary server. This means that if an accepted request does not match a servername instruction, it will be servos by the first virtualhost.

Attention
If you wish, you can replace the * with a certain IP address. In this case, the VirtualHost parameters must match the parameters of the Namevirtualhost:

The code is as follows Copy Code

Namevirtualhost 172.20.30.40

<virtualhost 172.20.30.40>
# etc ...

In any case, it is convenient to use * When your IP address is not certain-for example, your ISP configures you with a dynamic IP address, and you use a dynamic Domain name resolution system. Because * matches any IP address, in this case, no matter how the IP address changes, you do not need to configure another.

The above configuration is what you will use when using a domain-based virtual host in most cases. In fact, this configuration will not satisfy you in only one case: when you want to provide different content for different IP addresses or ports.

Use a domain-based virtual host for more than one IP.
Attention
Any technology discussed here can be extended to use any number of IP addresses.

The server has two IP addresses. One (172.20.30.40) is used for the "primary" server--server.domain.com and the other (172.20.30.50), which we will use to build two or more virtual hosts.

Server Configuration

  code is as follows copy code

Listen 80

# This is the ' main ' server running on 172.20.30.40
ServerName server.domain.com
Documentroot/www/mainser ver

# This are the other address
Namevirtualhost 172.20.30.50

<virtualhost 172.20.30.50>
Docum Entroot/www/example1
ServerName www.example1.com

# other directives ...

</virtualhost>

<virtualhost 172.20.30.50>
documentroot/www/example2
ServerName www.example2.org

# Other directives here ...

</virtualhost>

Any requests that are not for 172.20.30.50 will be servos by the primary server. A request submitted to 172.20.30.50 without a hostname or host: Header will be www.example1.com servo.

Provide the same content on different IP addresses, such as an internal and an external address.
The server has two IP addresses (192.168.1.1 and 172.20.30.40). This machine is located between the internal (local area network) and external (WAN) networks. On the outside, the domain name server.example.com to the external address (172.20.30.40), while inside, the same domain name points to the internal address (192.168.1.1).

The server can be configured to provide the same content to requests from both internal and external sources, and you need only one VirtualHost configuration segment to achieve this.

Server Configuration

The code is as follows Copy Code

Namevirtualhost 192.168.1.1
Namevirtualhost 172.20.30.40

<virtualhost 192.168.1.1 172.20.30.40>
Documentroot/www/server1
ServerName server.example.com
Serveralias Server
</VirtualHost>

Now, requests submitted from different networks will be servos by the same virtualhost.

Attention:
In the intranet, you can use the name of the server instead of the server.example.com full name.

As above, in the example above, you can replace the specific IP address with *, so you can return the same content to all the addresses.

Run a different site on a different port.
If you want to have different ports for the same IP to servos multiple domain names. You can do this by using a method such as defining a port in the "Namevirtualhost" tab. If you want to use <virtualhost name:port> without namevirtualhost name:port or direct with the listen directive, your configuration will not take effect.

Server Configuration

  code is as follows copy code

Listen-
Listen 8080

Namevirtualhost 172.20.30.40:80
Namevirtualhost 172.20.30.40:8080

<virtualhost 172.20.30.40:80>
ServerName www.example1.com
documentroot/www/domain-80
</virtualhost>

<virtualhost 172.20.30.40:8080>
ServerName www.example1.com
documentroot/www/domain-8080
</virtualhost>

<virtualhost 172.20.30.40:80
ServerName www.example2.org
documentroot/www/otherdomain-80
</virtualhost>

< VirtualHost 172.20.30.40:8080>
ServerName www.example2.org
documentroot/www/otherdomain-8080
</ Virtualhost>

Setting up an ip-based virtual host
A service configuration with two IP addresses (172.20.30.40 and 172.20.30.50) corresponding to the domain name www.example1.com and www.example2.org is as follows:

Server Configuration

The code is as follows Copy Code

Listen 80

<virtualhost 172.20.30.40>
Documentroot/www/example1
ServerName www.example1.com
</VirtualHost>

<virtualhost 172.20.30.50>
Documentroot/www/example2
ServerName www.example2.org
</VirtualHost>

If there is a primary server (major server), requests that do not appear in either of the <VirtualHost> directives (for example, requests for localhost) will be servos by the primary server.

Hybrid based on port and ip-based virtual hosts
If your server has two IP addresses (172.20.30.40 and 172.20.30.50) corresponding to the domain name www.example1.com and www.example2.org respectively. For each domain, you want to publish your site on port 80 and port 8080. You can configure this:

Server Configuration

  code is as follows copy code

Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080

<virtualhost 172.20.30.40:80>
documentroot/www/ example1-80
ServerName www.example1.com
</virtualhost>

<virtualhost 172.20.30.40:8080>
documentroot/www/example1-8080
ServerName www.example1.com
</virtualhost>

<virtualhost 172.20.30.50:80>
documentroot/www/example2-80
ServerName www.example1.org
</virtualhost>

<virtualhost 172.20.30.50:8080>
documentroot/www/example2-8080
ServerName www.example2.org
</virtualhost>

Hybrid domain-based and ip-based virtual hosts
If you want to configure a domain-based virtual host on some addresses and some other ip-based virtual hosts.

Server Configuration

The code is as follows Copy Code

Listen 80

Namevirtualhost 172.20.30.40

<virtualhost 172.20.30.40>
Documentroot/www/example1
ServerName www.example1.com
</VirtualHost>

<virtualhost 172.20.30.40>
Documentroot/www/example2
ServerName www.example2.org
</VirtualHost>

<virtualhost 172.20.30.40>
Documentroot/www/example3
ServerName www.example3.net
</VirtualHost>

# ip-based
<virtualhost 172.20.30.50>
Documentroot/www/example4
ServerName www.example4.edu
</VirtualHost>

<virtualhost 172.20.30.60>
Documentroot/www/example5
ServerName www.example5.gov
</VirtualHost>

Using the _default_ virtual host
Configure _default_ virtual hosts for all ports
This is configured to capture any requests that point to unspecified IP addresses and ports. For example, an address/port pair that is not used by any virtual host.

Server Configuration

The code is as follows Copy Code
<virtualhost _default_:*>
Documentroot/www/default
</VirtualHost>

Using such a default virtual host with a connector port can effectively prevent requests from being received by the primary server.

If an address/port pair is already in use by a domain-based virtual host, the default virtual host will never process requests sent to this address/port. If a requested host header contains unknown information, or simply does not, it is processed by the first domain-based virtual host (that is, a virtual host that first appears in the configuration file using that address/port pair).

You can rewrite any request with Aliasmatch or rewriterule to point to a simple information page (a single information page) (or script).

Deploying _default_ virtual hosts for different ports
Same as the first, but we want the server to listen on many ports while the second _default_ virtual host listens on port 80 alone.

Server Configuration

The code is as follows Copy Code

<virtualhost _default_:80>
Documentroot/www/default80
# ...
</VirtualHost>

<virtualhost _default_:*>
Documentroot/www/default
# ...
</VirtualHost>

A default virtual host that listens on port 80 (must appear before all virtual hosts that use the connector port) captures all requests sent to an unspecified IP address. The primary server will not be used for servo any requests.

Configuring a _default_ virtual host for one port
If we only want to set up a unique default virtual host on port 80, we should configure this:

Server Configuration

The code is as follows Copy Code
<virtualhost _default_:80>
Documentroot/www/default
...
</VirtualHost>

A request to port 80 that is sent to an unspecified address will be servo for this virtual host, while a request to another port that does not have a set address is sent to the main server.

Porting a domain-based virtual host to an ip-based virtual host
If a virtual host with a www.example2.org domain name (the second in a domain name configuration example) gets its own IP address. In order to avoid some domain name servers or proxy servers during the porting of this domain to do the old parsing, we can adopt a transition method: At the same time provide the old and new two IP address resolution.
It is easy to achieve this goal. Because we simply add the new address (172.20.30.50) to the VirtualHost command.

Server Configuration

The code is as follows Copy Code

Listen 80
ServerName www.example1.com
Documentroot/www/example1

Namevirtualhost 172.20.30.40

<virtualhost 172.20.30.40 172.20.30.50>
Documentroot/www/example2
ServerName www.example2.org
# ...
</VirtualHost>

<virtualhost 172.20.30.40>
Documentroot/www/example3
ServerName www.example3.net
Serveralias *.example3.net
# ...
</VirtualHost>

The virtual host can now access the new address (represented as an ip-based virtual host) and the old address (represented as a domain-based virtual host) at the same time.


Using the Serverpath directive
If we run two domain-based virtual hosts on a single server. In order to match the correct virtual host, the client must send the correct host: header. The old client that uses http/1.0 cannot send such headers, so that Apache cannot tell which virtual host the client wants to connect to (the request will be servo-hosted by the primary virtual host). In order to provide backward compatibility, we can provide a primary virtual host to return a page, adding a link to the URL prefix of a domain-based virtual host on the page.

The code is as follows Copy Code

Server Configuration
Namevirtualhost 172.20.30.40

<virtualhost 172.20.30.40>
# primary Vhost
Documentroot/www/subdomain
Rewriteengine on
rewriterule ^/.*/www/subdomain/index.html
# ...
</virtualhost>

<virtualhost 172.20.30.40>
documentroot/www/subdomain/sub1
ServerName www.sub1.domain.tld
serverpath/sub1/
Rewriteengine on
rewriterule ^ (/sub1/.*)/www/subdomain$1
# ...
</virtualhost>

<virtualhost 172.20.30.40>
documentroot/www/subdomain/sub2
ServerName www.sub2.domain.tld
serverpath/sub2/
Rewriteengine on
rewriterule ^ (/sub2/.*)/www/subdomain$1
# ...
</virtualhost>

Because of the role of the Serverpath directive, requests sent to HTTP://WWW.SUB1.DOMAIN.TLD/SUB1/are always servo-sub1-vhost.
If the client sends the correct host: header, the command sent to http://www.sub1.domain.tld/is Sub1-vhost servo. If no host: header is sent, the client receives the information page sent from the primary virtual host.
Note that there is a small problem here: If the client does not send a host: header, the request to send to http://www.sub2.domain.tld/sub1/will still be sub1-vhost servo.

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.