Virtual hosts

Contents

Use vhosts to safely expose services (sites/webapps etc) through public domains.

Service mapping

Vhosts essentially serve two purposes, traffic routing and mapping to ID providers.

A single XP runtime instance may offer multiple services. By default, XP provides the following endpoints:

  • localhost:8080/webapp/<app-name>/ (webapp engine)

  • localhost:8080/site/<repo>/<branch>/<path> (site engine)

  • localhost:8080/admin/ (admin engine)

By using vhosts, traffic for specific domains and url patterns may be routed directly to defined service paths in XP.

I.e. example.com -> :8080/site/default/master/homepage

XP XP 7.8.0 7.8.0 Domains can be defined as single host, multiple hosts separated by space and/or regular expressions (started with ~ symbol).

IDN hosts should be specified using Punycode representation.

With vhosts enabled, unexposed services are no longer accessible.

Vhosts may also define the relationship to one or more ID providers. This enables pluggable authentication on a per-vhost basis.

Vhosts only apply to XP service port (defaults to 8080), not the monitoring and management ports.

Request Matching

VHost matches the request if host value matches the requested host AND request-uri matches source.

XP XP 7.8.0 7.8.0 Host matching can be an exact match or regular expression match. Read more about Java Regular Expressions at Pattern JavaDoc Host matching is case-insensitive in both cases.

For backwards compatibility with old versions of XP root domain is not normalized in host. Specify both host values mapping.example.host = example.com. example.com if case you need to support FQDN-style host.
XP XP 7.8.0 7.8.0 host mapping match both www.example.com and example.com hosts
mapping.example.host = www.example.com example.com
XP XP 7.8.0 7.8.0 host mapping will match any sub-domain: 1.example.com, 2.example.com hosts (but not sub-sub-domains)
mapping.example.host = ~[^.]+\\.example\\.com

Source matching is done with "exact" or "starts-with" match. Source matching is case-sensitive, and is done before URI decoding.

source mapping matches any request-uri
mapping.example.source = /
source mapping matches any request-uri that equals to /hello or starts with /hello/
mapping.example.source = /hello

If multiple VHosts match the request, disambiguation rules apply in the following order:

  • XP XP 7.8.0 7.8.0 VHost with lower order value is preferred. If order is not specified, order is considered least preferable (Integer.MAX_VALUE).

  • VHost with the longest source value is preferred.

  • arbitrary VHost is selected.

Capture Groups XP XP 7.8.0 7.8.0

When VHost matched by a host regular expression, capture groups can be used in target via group references.

mapping.example.host = ~example\\.(?<tld>[^.]+)
mapping.example.target = /site/default/master/${tld}

In this example if host is example.no, then target becomes /site/default/master/no

Avoid mixing regular expressions, multiple hosts and capture groups. If host is not matched by regular expression, then group references in target will remain as-is.

Examples

"Example Inc", the owner of "example.com" just finished building their new site. The published site is available on <host>:8080/site/default/master/homepage.

In this case, a vhost configuration looking like this will get the job done:

Sample vhost config file
enabled = true

mapping.example.host = example.com
mapping.example.source = /
mapping.example.target = /site/default/master/homepage

After saving the vhost config file, you should see the following line the XP log:

2019-05-10 11:34:17,234 INFO  c.e.x.w.v.i.c.VirtualHostConfigImpl - Virtual host is enabled and mappings updated.
Each mapping must have a unique mapping identifier, in this case we used example.

Also, "Example Inc" wants the admin console deployed on example.com/admin. To solve this, we will simply add another mapping to the config:

mapping.adm.host = example.com
mapping.adm.source = /admin
mapping.adm.target = /admin
mapping.adm.idProvider.system = default

This time we also added an ID provider to the mapping. This effectively activates system the default (and only) ID provider for this vhost.

Testing vhosts

To verify that your vhost config is working without setting up proxies or modifying your DNS: Simply add the following line to your hosts file.

<host-ip-address>     example.com
Location of hosts file on Mac/Linux_: /etc/hosts, on Windows: c:\Windows\System32\Drivers\etc\hosts

Pointing your browser to http://example.com:8080 will reveal the glorious result.

Visit the vhost configuration section for more details.


Contents

Contents