Caucho maker of Resin Server | Application Server (Java EE Certified) and Web Server


 

Resin Documentation

home company docs 
app server 
 Resin Server | Application Server (Java EE Certified) and Web Server
 

virtual hosting


A Resin server can serve many virtual hosts, each with its own servlets and documents. The configuration is flexible, allowing dynamic host deployment in a hosts directory or using explicit <host> tags for additional control and security, and compatibility with existing Apache sites, enabling easy upgrades and evaluation for PHP servers to Quercus.

Overview

Virtual hosts are multiple internet domains served by the same Resin server. Because one JVM handles all the domains, its more memory and processing efficient, as well as sharing IP addresses. With Resin, adding virtual hosts can as easy as creating a directory like /var/resin/hosts/foo.com and setting up the DNS name. Explicit virtual host is also possible to match existing layouts, like matching a /var/resin/htdocs configuration when migrating a PHP mediawiki or wordpress site to use Quercus for security and performance.

The virtual host will contain one or more web-apps to serve the host's contents. Simple sites will use a fixed root webapp, like the Apache-style /var/resin/htdocs. More complicated sites can use a webapps-style directory.

Each virtual host belongs to a Resin <cluster>, even if the cluster has only a single server.

For example, a Resin server might manage both the www.gryffindor.com and www.slytherin.com domains, storing the content in separate directories (/var/resin/gryffindor and /var/resin/slytherin), and using a single IP address for both domains. In this scenario, both www.gryffindor.com and www.slytherin.com are registered with the standard domain name service registry as having the IP address 192.168.0.13. When a user types in the url http://www.gryffindor.com/hello.jsp in their browser, the browser will send the HTTP request to the IP address 192.168.0.13 and send an additional HTTP header for the gryffindor host, "Host: www.gryffindor.com". When Resin receives the request it will grab the host header, and dispatch the request to the configured virtual host.

Example: HTTP request headers
C: GET /test.jsp HTTP/1.1
C: Host: www.gryffindor.com
C:
  1. host name
  2. host aliases
  3. optional host.xml
  4. root directory
  5. web-applications
  6. configuration environment
  7. logging

Dynamic virtual hosts

Resin can deploy virtual hosts automatically by scanning a host deployment directory for virtual host content. Each sub-directory in the hosts directory will cause Resin to create a new virtual host. To customize the configuration, you can add a host.xml in the host's root directory for shared databases, beans or security, or to add <host-alias> names.

You can add hosts dynamically to a running server just by creating a new host directory. Resin periodically scans the hosts directory looking for directory changes. When it detects a new host directory, it will automatically start serving files from the new virtual hosts.

If you add a default directory in the hosts, Resin will use it to serve all unknown virtual hosts. The default host is handy for simple servers with only a single virtual host and for sites where the virtual host is handled in software, like Drupal. If the default directory is missing, Resin will return 404 Not Found for any unknown virtual hosts.

Example: virtual host directory structure
/var/resin/hosts/www.gryffindor.com/
                                 host.xml
                                 log/access.log
                                 webapps/ROOT/index.jsp
                                 webapps/ROOT/WEB-INF/resin-web.xml
				 
/var/resin/hosts/www.slytherin.com/
                                host.xml
                                log/access.log
                                webapps/ROOT/index.php
                                webapps/ROOT/WEB-INF/resin-web.xml
				 
/var/resin/hosts/default/
                      host.xml
                      log/access.log
                      webapps/ROOT/index.php
                      webapps/ROOT/WEB-INF/resin-web.xml

host-aliasing for dynamic hosts

Often, the same virtual host will respond to multiple names, like www.slytherin.com and slytherin.com. One name is the primary name and the others are aliases. In Resin, the primary name is configured by the <host-name> tag and aliases are configured by <host-alias>. In a dynamic host configuration, the directory name is used as the host-name by default, and aliases are declared in the host.xml.

Example: www.slytherin.com/host.xml
<host xmlns="http://caucho.com/ns/resin">
  <host-name>www.slytherin.com</host-name>
  <host-alias>slytherin.com</host-alias>
  <host-alias>quidditch.slytherin.com</host-alias>
</host>

Since the host.xml is shared for all web-applications in the host, you can also use it to configure shared resources like security logins, shared databases, and shared resources.

host-deploy configuration

The <host-deploy> tag configures the dynamic virtual hosting specifying the directory where Resin should scan for virtual hosts. Because Resin does not automatically add default configuration, you will need to also add configuration for the host.xml, app-default.xml and web-app-deploy. Although it's a bit more verbose, the no-default rule makes Resin more secure and debuggable. If an item like a <web-app> is missing, Resin will return 404 Not Found for security. Because all configuration is explicit, it's ultimately traceable to the resin.xml which makes debugging more reliable.

Shared host configuration goes in the <host-default> tag. In this case, we've added an optional host.xml for configuration, an access log in log/access.log and a standard webapps directory. The standard servlets and file handling come from the app-default.xml file. If you omit either the app-default.xml or the webapps, you will see 404 Not Found for any requests.

The example below is a complete, working resin.xml listening to HTTP at port 8080. The cluster consists of a single server. It includes a <development-mode-error-page> to help debugging the configuration. Many sites will omit the error-page to hide configuration details in case an error occurs on a live site.

Example: /etc/resin/resin.xml host-deploy configuration
<resin xmlns="http://caucho.com/ns/resin"
          xmlns:resin="urn:java:com.caucho.resin">
<cluster id="app-tier">
  <server id="app-a" address="192.168.1.13" port="6800">
    <http port="8080"/>
  </server>

  <development-mode-error-page/>

  <resin:import path="${__DIR__}/app-default.xml"/>
  
  <host-default>
    <resin:import path="host.xml" optional="true"/>

    <access-log path="log/access.log"/>
	
    <web-app-deploy path="webapps"
           expand-preserve-fileset="WEB-INF/work/**"/>
  </host-default>

  <host-deploy path="hosts">
  </host-deploy>
  
</cluster>
</resin>

Any directory created in ${resin.root}/hosts will now become a virtual host. You can also place a .jar file in ${resin.root}/hosts, it is expanded to become a virtual host.

${resin.root}/hosts/www.gryffindor.com/
${resin.root}/hosts/www.gryffindor.com/webapps/ROOT/index.jsp
${resin.root}/hosts/www.gryffindor.com/webapps/foo/index.jsp

${resin.root}/hosts/www.slytherin.com.jar

Jar libraries and class files that are shared amongst all webapps in the host can be placed in lib and classes subdirectories of the host:

${resin.root}/hosts/www.gryffindor.com/lib/mysql-connector-java-3.1.0-alpha-bin.jar 
${resin.root}/hosts/www.gryffindor.com/classes/example/CustomAuthenticator.java

More information is available in the configuration documentation for <host-deploy> and <host-default>.

Explicit Virtual Hosting

In a more structured site, you can take complete control of the virtual host configuration and configure each virtual host explicitly. Existing sites wanting to upgrade to Resin or sites with extra security needs may prefer to configure each <host> in the resin.xml. For example, a PHP Drupal site evaluating Quercus to improve performance and security might use the explicit <host> to point to the existing /var/resin/htdocs directory.

In the explicit configuration, each virtual host has its own host block. At the very least, each host will define the id specifying the host name and a root web-app. A <root-directory> is often used to provide a host specific root for logfiles.

As with the dynamic hosting, servlets and web-apps must be configured either in a <host-default> or explicitly. If they are missing, Resin will return a 404 Not Found for security. The host id="" is the default host and will serve any request that doesn't match other hosts. If you don't have a default host, Resin will return a 404 Not Found for any unknown host.

The following sample configuration defines an explicit virtual hosts www.slytherin.com and a default host, each with its own root directory, access-log and a single explicit <web-app> in the htdocs directory. The default virtual host is configured just like a typical Apache configuration, so it can be used to upgrade an Apache/PHP site to use Quercus for security and performance.

Example: /etc/resin/resin.xml
<resin xmlns="http://caucho.com/ns/resin"
        xmlns:resin="urn:java:com.caucho.resin">
        
<cluster id="app-tier">
  <server id="app-a" address="192.168.1.10" port="6800">
    <http port="8080"/>
  </server>

  <development-mode-error-page/>

  <resin:import path="${__DIR__}/app-default.xml"/>

  <host id="">
    <root-directory>/var/resin</root-directory>

    <access-log path="logs/access.log"/>

    <web-app id="" root-directory="htdocs"/>
  </host>

  <host id="www.slytherin.com">
    <host-alias>slytherin.com</host-alias>
    
    <root-directory>/var/slytherin</root-directory>

    <access-log path="logs/access.log"/>

    <web-app id="" root-directory="htdocs"/>
  </host>

</cluster>
</resin>

Browsing http://gryffindor.caucho.com/test.php will look for /var/resin/htdocs/test.php.

Browsing http://slytherin.caucho.com/test.php will look for /var/slytherin/htdocs/test.php.

Server per virtual host

In some ISP setups, it may make sense to assign a server for each virtual host. The isolation of web-apps may not be sufficient; each host needs a separate JVM. In this configuration, each <host> belongs to its own <cluster> and has a dedicated <server>. Normally, this configuration will operate using load-balancing, so the load-balance server will dispatch requests as appropriate.

For further security restrictions, see the watchdog section. ISPs can also use the watchdog to assign different <user-name> values for each host and can even create chroot directories for each JVM.

A front-end web server receives all requests, and is configured to dispatch to back-end Resin server that correspond to the host name.

client <-> front-end webserver <-> (resin:6802(jvm), resin:6803(jvm))

Back-end JVMs

Each host is placed in its own <cluster> with a dedicated <server>. Since the server listens to a TCP port for load-balancing and clustering messages, each server on the machine needs a different server port.

In this example, the virtual hosts www.gryffindor.com and www.slytherin.com each get their own server. The backend clusters have their own virtual host. The frontend load-balancer dispatches the <resin:LoadBalance> tags to the backend.

This example is split into two blocks to emphasize the frontend and backend. Typically, they will both actually be in the same resin.xml to ensure consistency.

Example: /etc/resin/resin.xml for backend
<resin xmlns="http://caucho.com/ns/resin"
          xmlns:resin="urn:java:com.caucho.resin">

  <cluster-default>
    <resin:import path="${resin.home}/conf/app-default.xml"/>
    
    <host-default>
      <web-app-deploy path="webapps"
           expand-preserve-fileset="WEB-INF/work/**"/>
    </host-default>
  </cluster-default>

  <cluster id="gryffindor>
    <server id="gryffindor" host="localhost" port="6800"/>

    <host id="www.gryffindor.com">
  
      <root-directory>/var/resin/gryffindor</root-directory>

    </host>
  </cluster>

  <cluster id="slytherin">
    <server id="slytherin" host="localhost" port="6801"/>

    <host id="www.slytherin.com">
  
      <root-directory>/var/resin/slytherin</root-directory>

    </host>
  </cluster>

  <cluster id="web-tier">
    <!-- see below -->
    ...
  </cluster>

</resin>

Each back-end server is started separately:

Example: starting backend servers
unix> bin/resin.sh -server gryffindor start
unix> bin/resin.sh -server slytherin start
Example: stopping backend servers
unix> bin/resin.sh -server gryffindor stop
unix> bin/resin.sh -server slytherin stop

Resin web-tier load balancer

The host-specific back-end servers are ready to receive requests on their server ports. A third Resin server can be used as the front-end load-balancer. It receives all requests and dispatches to the back-end servers.

client <-> Resin front-end webserver <-> (resin:6802(jvm), resin:6803(jvm))

The Resin web server is configured using rewrite with a <resin:LoadBalance directive to dispatch to the back-end server. A cluster is defined for each back-end host, so that the <load-balance> knows how to find them.

Example: /etc/resin/resin.xml for front-end web server
<resin xmlns="http://caucho.com/ns/resin"
     xmlns:resin="urn:java:com.caucho.resin">
     
  <cluster id="web-tier">
    <server-default>
      <http port="80"/>
    </server-default>

    <server id="web" address="192.168.2.1" port="6800"/>

    <host id="gryffindor.com">
      <web-app id="/">

        <resin:LoadBalance regexp="" cluster="gryffindor"/>

      </web-app>
    </host>

    <host id="slytherin.com">
      <web-app id="/">

        <resin:LoadBalance regexp="" cluster="slytherin"/>

      </web-app>
    </host>
  </cluster>

  <cluster id="gryffindor">
    <server id="gryffindor" address="192.168.2.2" port="6800"/>

    <host id="www.gryffindor.com">
      ...
    </host>
  </cluster>

  <cluster id="slytherin">
    <server id="slytherin" address="192.168.2.2" port="6801"/>

    ...
  </cluster>
</resin>

Starting the servers on Unix

The front-end server JVM is started similar to the back-end JVMs:

Example: starting the load balancer
unix> bin/resin.sh -server web -conf conf/resin.xml start
...
unix> bin/resin.sh -server web -conf conf/resin.xml stop

Starting the servers on Windows

With Windows, each JVM is installed as a service. Service is installed using setup.exe graphical utility. It's possible to install a number of Resin services each using a unique name. The name will need to be supplied into 'Service Name' field.

Resin setup.exe screenshot

You will either need to reboot the machine or start the service from the Control Panel/Services panel to start the server. On a machine reboot, NT will automatically start the service.

There is a bug in many JDKs which cause the JDK to exit when the administrator logs out. JDK 1.4 and later can avoid that bug if the JDK is started with -Xrs.

Configuration tasks

host naming

The virtual host name can be configured by an explicit <host-name>, a <host-alias>, a <host-alias-regexp>, by the <host> tag or implicitly by the <host-deploy>. For explicit configuration styles, the host name and alias configuration will generally be in the resin.xml. For dynamic configuration, the host aliases will typically be in an included host.xml inside the host directory.

The default host catches all unmatches hosts. Simpler sites will use the default host for all requests, while security-conscious sites may remove the default host entirely. If the default host is not configured, Resin will return a 404 Not Found.

host.xml

The host.xml is an optional file where virtual hosts can put host-common configuration. The host.xml is a good place for shared resources like authentication, database pools or host-wide beans and services. It's also a location for the <host-alias> in a dynamic hosting configuration.

The host.xml is configured in a <host-deploy> or <host-default> by adding a <resin:import> tag specifying the host.xml name and location. Because the <host-default> applies the <resin:import> to every virtual host, it becomes a common system-wide configuration file.

web-applications

Hosts must define web-apps in order to serve files, servlets, or PHP pages. If the host is missing all webapps, Resin will return 404 Not Found for all requests make to the host.

Both explicit <web-app> and dynamic web-app-deploy tags are used to configure webapps. The explicit style is generally used for Apache-style configuration, while the dynamic style is generally used for Java app-server .war configuration.

Remember, Resin's default servlets like the file, JSP, and PHP servlets also need to be defined before they're used. So all Resin configuration files need to have a <resin:import> of the conf/app-default.xml configuration file either in the <cluster> or in a shared <cluster-default>. If the app-default.xml is missing, Resin will not serve static files, JSP, or PHP, and will not even look in the WEB-INF for resin-web.xml, classes, or lib.

IP-Based Virtual Hosting

While Resin's virtual hosting is primarily aimed at named-based virtual hosts, it's possible to run Resin with IP-Based virtual hosts.

With IP virtual hosting, each <http> block is configured with the virtual host name. This configuration will override any virtual host supplied by the browser.

<resin xmlns="http://caucho.com/ns/resin">

<cluster id="web-tier">
  <server id="a">

    <http address="192.168.0.1" port="80"
          virtual-host="slytherin.caucho.com"/>

    <http address="192.168.0.2" port="80"
          virtual-host="gryffindor.caucho.com"/>

  </server>

  ...

  <host id="slytherin.caucho.com">
    ...
  </host>
</cluster>
</resin>

Internationalization

Resin's virtual hosting understands host names encoded using rfc3490 (Internationalizing Domain Names in Applications). This support should be transparent. Just specify the virtual host as usual, and Resin will translate the brower's encoded host name the unicode string.

Support, of course, depends on the browser. Mozilla 1.4 supports the encoding.

Virtual Hosts with Apache or IIS

A common configuration uses virtual hosts with Apache or IIS. As usual, Apache or IIS will pass matching requests to Resin.

Apache

The Resin JVM configuration with Apache is identical to the standalone configuration. That similarity makes it easy to debug the Apache configuration by retreating to Resin standalone if needed.

The ServerName directive in Apache with the UseCanonicalName can be used to select a canonical name for the virtual host virtual hosting work. When Apache passes the request to Resin, it tells Resin the ServerName. Without the ServerName, Apache will use the "Host:" header in the HTTP request to select which host to serve.

httpd.conf
LoadModule caucho_module /usr/local/apache/libexec/mod_caucho.so

ResinConfigServer localhost 6802

UseCanonicalName on

<VirtualHost 127.0.0.1>
  ServerName gryffindor.caucho.com
</VirtualHost>

<VirtualHost 192.168.0.1>
  ServerName slytherin.caucho.com
</VirtualHost>
note
Note
You'll the LoadModule must appear before the ResinConfigServer for Apache to properly understand the ResinConfigServer command. If they're missing, Apache will send an error.

Apache front-end

The host-specific back-end JVMs are ready to receive requests on their server ports. Apache is the front-end server, and is configured to dispatch to the appropriate back-end Resin JVM for the host:

client <-> Apache <-> (resin:6802(jvm), resin:6803(jvm))
httpd.conf
UseCanonicalName on

<VirtualHost 127.0.0.1>
  ServerName gryffindor.caucho.com
  ResinConfigServer 192.168.0.10 6800
</VirtualHost>

<VirtualHost 192.168.0.1>
  ServerName slytherin.caucho.com
  ResinConfigServer 192.168.0.11 6800
</VirtualHost>

When you restart the Apache web server, you can look at http://gryffindor/caucho-status and http://slytherin/caucho-status to check your configuration. Check that each virtual host is using the server address and port that you expect.

Testing virtual hosts

During development and testing, it is often inconvenient or impossible to use real virtual host names that are registered as internet sites, and resolve to an internet-available IP address. OS-level features on the test client machine can be used to map a virtual host name to an IP address.

For example, developers often run the Resin server and the test client (usually a browser) on the same machine. The OS is configured to map the "www.gryffindor.com" and "www.slytherin.com" names to "127.0.0.1", pointing these host names back to computer that the client is running on.

Unix users edit the file /etc/hosts:

/etc/hosts
127.0.0.1       localhost

127.0.0.1       www.gryffindor.com
127.0.0.1       www.slytherin.com

Windows user edit the file C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS:

C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS
127.0.0.1       localhost

127.0.0.1       www.gryffindor.com
127.0.0.1       www.slytherin.com

Deployment

Overriding web-app-deploy configuration

The web-app-deploy can override configuration for an expanded war with a matching <web-app> inside the <web-app-deploy>. The <document-directory> is used to match web-apps.

Example: resin.xml overriding web.xml
<resin xmlns="http://caucho.com/ns/resin">
<cluster id="">
<host id="">

<web-app-deploy path="webapps">
  <web-app context-path="/wiki"
              document-directory="wiki">
    <context-param database="jdbc/wiki">
  </web-app>
</web-app-deploy>

</host>
</cluster>
</resin>

versioning

The versioning attribute of the <web-app-deploy> tag improves web-app version updates by enabling a graceful update of sessions. The web-apps are named with numeric suffixes, e.g. foo-10, foo-11, etc, and can be browsed as /foo. When a new version of the web-app is deployed, Resin continues to send current session requests to the previous web-app. New sessions go to the new web-app version. So users will not be aware of the application upgrade.


Copyright © 1998-2015 Caucho Technology, Inc. All rights reserved. Resin ® is a registered trademark. Quercustm, and Hessiantm are trademarks of Caucho Technology.