Index Next: Getting Started With SQLite

Virtual Hosts

Add Hosts

Using virtual hosts allows multiple websites to be hosted off of a single instance of JaxServer.

To demonstrate the virtual host functionality, new hostnames need to be mapped to the local computer at 127.0.0.1. Edit the /etc/hosts file and add two fictional domains helloworld.test and welcome.test:

Source of /etc/hosts:
# ...
# add test hostnames
127.0.0.1       helloworld.test
127.0.0.1       welcome.test

Test that the host names work:

ping helloworld.test
ping otherdomain.test

The goal is to run both the welcome app and the existing helloworld app simultaneously from the same instance of JaxServer. To do this the vhosts.js configuration file needs to be modified to map the helloworld application to the helloworld.test domain, and map the otherdomainapp application to the otherdomain.test domain:

Source of ~/jaxserver/conf/vhosts.js:
exports.vhosts = function() {
	return {
		
		// vhost applications and log file locations
		apps : {
			'helloworld' : {
				accessLogFile : jax.config.logDir + '/helloworld-access.log'
			},
			'welcome' : {
				accessLogFile : jax.config.logDir + '/welcome-access.log'
			}
		},
		
		// map hostname to application
		hostnames : {
			'helloworld.test'	: 'helloworld',
			'welcome.test'	: 'welcome'
		},
		
		// default app is displayed when requesting any other hostname or ip address
		defaultApp : 'jaxcore'
		
	};
};

Vhosts Application

Start the vhosts application:

jax run vhosts

Test the applications:

Vhost Configuration File

Instead of having a single virtual host configuration file. Many can be defined and selectively chosen when starting up the vhosts app. Because the configuration is contained in a JavaScript function, logic can be used to create different configurations based on the environment.

Source of ~/jaxserver/conf/vhosts-mydomains.js:
exports.vhosts = function() {
	
	var hosts = {}:
	
	// vhost config for development mode
	if (jax.config.env=='dev') {
		hosts['exampleapp1.test'] = 'exampleapp1';
		hosts['exampleapp2.test'] = 'exampleapp2';
	}
	if (jax.config.env=='prod') {
		hosts['exampleapp1.com'] = 'exampleapp1';
		hosts['www.exampleapp1.com'] = 'exampleapp1';
		hosts['exampleapp2.com'] = 'exampleapp2';
		hosts['www.exampleapp2.com'] = 'exampleapp2';
	}

	return {
		apps : {
			'exampleapp1' : {
				accessLogFile : jax.config.logDir + '/exampleapp1-access.log'
			},
			'exampleapp2' : {
				accessLogFile : jax.config.logDir + '/exampleapp2-access.log'
			}
		},
		hostnames : hosts,
		defaultApp : 'exampleapp1'
	};
};
jax run vhosts --vhostConfig=~/jaxserver/conf/vhost-mydomains.js --env=prod
Next: Getting Started With SQLite