Expressjs/vhost: virtual domain hosting

This document attempts lớn answer the commonly-asked questions about setting up virtual hosts. These scenarios are those involving multiple web sites running on a single server, via name-based or IP-based virtual hosts.


See also

Running several name-based website sites on a single IP address.

Bạn đang xem: Expressjs/vhost: virtual domain hosting

Your VPS has multiple hostnames that resolve sầu to lớn a single address, và you want to respond differently for &


Creating virtual host configurations on your Apabít hệ thống does not magically cause DNS entries to be created for those host names. You must have sầu the names in DNS, resolving to lớn your IP.. address, or nobody toàn thân else will be able to see your web site. You can put entries in your hosts file for local testing, but that will work only from the machine with those hosts entries.

# Ensure that listens on port 80Listen 80 DocumentRoot "/www/example1" ServerName # Other directives here DocumentRoot "/www/example2" ServerName # Other directives here The asterisks match all addresses, so the main server serves no requests. Due to the fact that the virtual host with ServerName is first in the configuration tệp tin, it has the highest priority and can be seen as the default or primary hệ thống. That means that if a request is received that does not match one of the specified ServerName directives, it will be served by this first .

The above configuration is what you will want lớn use in almost all name-based virtual hosting situations. The only thing that this configuration will not work for, in fact, is when you are serving different nội dung based on differing IP. addresses or ports.


You may replace * with a specific IP address on the system. Such virtual hosts will only be used for HTTPhường requests received on connection to the specified IPhường address.

However, it is additionally useful khổng lồ use * on systems where the IP address is not predictable - for example if you have sầu a dynamic IP. address with your ISP, and you are using some variety of dynamic DNS solution. Since * matches any IP address, this configuration would work without changes whenever your IPhường. address changes.

Name-based hosts on more than one IPhường address.


Any of the techniques discussed here can be extended to lớn any number of IPhường. addresses.

The server has two IP. addresses. On one (172.trăng tròn.30.40), we will serve the "main" VPS, và on the other (, we will serve sầu two or more virtual hosts.

Listen 80# This is the "main" hệ thống running on 172.đôi mươi.30.40ServerName hệ thống.example.comDocumentRoot "/www/mainserver" DocumentRoot "/www/example1" ServerName # Other directives here ... DocumentRoot "/www/example2" ServerName # Other directives here ... Any request to lớn an address other than 172.trăng tròn.30.50 will be served from the main server. A request lớn with an unknown hostname, or no Host: header, will be served from

Serving the same content on different IP addresses (such as an internal và external address).

The hệ thống machine has two IPhường. addresses ( & 172.trăng tròn.30.40). The machine is sitting between an internal (intranet) network and an external (internet) network. Outside of the network, the name resolves to lớn the external address (172.đôi mươi.30.40), but inside the network, that same name resolves to the internal address (

The VPS can be made to lớn respond khổng lồ internal & external requests with the same nội dung, with just one section.

DocumentRoot "/www/server1" ServerName hệ thố ServerAlias server Now requests from both networks will be served from the same .


On the internal network, one can just use the name hệ thống rather than the fully qualified host name

cảnh báo also that, in the above sầu example, you can replace the danh sách of IPhường addresses with *, which will cause the server khổng lồ respond the same on all addresses.

Running different sites on different ports.

You have sầu multiple domains going lớn the same IP và also want to lớn serve sầu multiple ports. The example below illustrates that the name-matching takes place after the best matching IP address & port combination is determined.

Listen 80Listen 8080 ServerName DocumentRoot "/www/domain-80" ServerName DocumentRoot "/www/domain-8080" ServerName DocumentRoot "/www/otherdomain-80" ServerName DocumentRoot "/www/otherdomain-8080"

IP-based virtual hosting

The VPS has two IP.. addresses (172.đôi mươi.30.40 and 172.trăng tròn.30.50) which resolve sầu khổng lồ the names & respectively.

Listen 80 DocumentRoot "/www/example1" ServerName DocumentRoot "/www/example2" ServerName Requests for any address not specified in one of the directives (such as localhost, for example) will go to the main server, if there is one.

Xem thêm: Thảo Luận Các Phần Mềm Dự Toán Delta Full Crack Phần Mềm Dự Toán Delta 8

Mixed port-based and ip-based virtual hosts

The VPS machine has two IP addresses (172.đôi mươi.30.40 and 172.trăng tròn.30.50) which resolve khổng lồ the names and respectively. In each case, we want lớn run hosts on ports 80 & 8080.

Listen 172.trăng tròn.30.40:80Listen 172.đôi mươi.30.50:80Listen 172.đôi mươi.30.50:8080 DocumentRoot "/www/example1-80" ServerName DocumentRoot "/www/example1-8080" ServerName DocumentRoot "/www/example2-80" ServerName DocumentRoot "/www/example2-8080" ServerName

Mixed name-based và IP-based vhosts

Any address mentioned in the argument lớn a virtualhost that never appears in another virtual host is a strictly IP-based virtual host.

Listen 80 DocumentRoot "/www/example1" ServerName DocumentRoot "/www/example2" ServerName DocumentRoot "/www/example3" ServerName IP-based DocumentRoot "/www/example4" ServerName DocumentRoot "/www/example5" ServerName

Using Virtual_host & mod_proxy together

The following example allows a front-end machine to proxy a virtual host through lớn a VPS running on another machine. In the example, a virtual host of the same name is configured on a machine at The ProxyPreserveHost On directive is used so that the desired hostname is passed through, in case we are proxying multiple hostnames to a single machine.

ProxyPreserveHost On ProxyPass "/" "" ProxyPassReverse "/" "" ServerName

Using _default_ vhosts

_default_ vhosts for all ports

Catching every request to any unspecified IP address & port, i.e., an address/port combination that is not used for any other virtual host.

DocumentRoot "/www/default" Using such a mặc định vhost with a wildthẻ port effectively prevents any request going to lớn the main server.

A mặc định vhost never serves a request that was sent to lớn an address/port that is used for name-based vhosts. If the request contained an unknown or no Host: header it is always served from the primary name-based vhost (the vhost for that address/port appearing first in the configuration file).

You can use AliasMatch or RewriteRule khổng lồ rewrite any request to a single information page (or script).

_default_ vhosts for different ports

Same as thiết đặt 1, but the hệ thống listens on several ports and we want lớn use a second _default_ vhost for port 80.

DocumentRoot "/www/default80" # ... DocumentRoot "/www/default" # ... The mặc định vhost for port 80 (which must appear before any mặc định vhost with a wildcard port) catches all requests that were sent to an unspecified IP address. The main hệ thống is never used to serve a request.

_default_ vhosts for one port

We want to lớn have a mặc định vhost for port 80, but no other mặc định vhosts.

DocumentRoot "/www/default"... A request to lớn an unspecified address on port 80 is served from the default vhost. Any other request lớn an unspecified address and port is served from the main hệ thống.

Any use of * in a virtual host declaration will have higher precedence than _default_.

Migrating a name-based vhost to an IP-based vhost

The name-based vhost with the hostname (from our name-based example, cài đặt 2) should get its own IPhường address. To avoid problems with name servers or proxies who cached the old IP address for the name-based vhost we want to lớn provide both variants during a migration phase.

The solution is easy, because we can simply add the new IP. address (172.trăng tròn.30.50) lớn the VirtualHost directive.

Listen 80ServerName www.example.comDocumentRoot "/www/example1" DocumentRoot "/www/example2" ServerName # ... DocumentRoot "/www/example3" ServerName ServerAlias * # ... The vhost can now be accessed through the new address (as an IP-based vhost) & through the old address (as a name-based vhost).

Using the ServerPath directive

We have a server with two name-based vhosts. In order to match the correct virtual host a client must skết thúc the correct Host: header. Old HTTP/1.0 clients do not skết thúc such a header and Apabít has no clue what vhost the client tried khổng lồ reach (& serves the request from the primary vhost). To provide as much backward compatibility as possible we create a primary vhost which returns a single page containing link with an URL prefix to the name-based virtual hosts.

# primary vhost DocumentRoot "/www/subdomain" RewriteEngine On RewriteRule "." "/www/subdomain/index.html" # ... DocumentRoot "/www/subdomain/sub1" ServerName www.sub1.domain.tld ServerPath "/sub1/" RewriteEngine On RewriteRule "^(/sub1/.*)" "/www/subdomain$1" # ... DocumentRoot "/www/subdomain/sub2" ServerName www.sub2.domain.tld ServerPath "/sub2/" RewriteEngine On RewriteRule "^(/sub2/.*)" "/www/subdomain$1" # ... Due to lớn the ServerPath directive a request to lớn the URL http://www.sub1.tên miền.tld/sub1/ is always served from the sub1-vhost. A request to lớn the URL http://www.sub1.domain.tld/ is only served from the sub1-vhost if the client sent a correct Host: header. If no Host: header is sent the client gets the information page from the primary host.

Please note that there is one oddity: A request to http://www.sub2.domain name.tld/sub1/ is also served from the sub1-vhost if the client sent no Host: header.

The RewriteRule directives are used khổng lồ make sure that a client which sent a correct Host: header can use both URL variants, i.e.

Xem thêm: Cách Bấm Số Phức Trên Máy Tính Fx 570Vn Plus Archives, Toán Số Phức Cmplx Trên Máy Fx

, with or without URL prefix.

Chuyên mục: Hosting