Created
May 27, 2012 16:58
-
-
Save azampagl/2815075 to your computer and use it in GitHub Desktop.
Joyent - NodeJS Smart Machine - Multiple application with http/https (SNI browser support required)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var https = require('https'); | |
var httpProxy = require('http-proxy'); | |
var crypto = require('crypto'); | |
var fs = require('fs'); | |
/* | |
* Server configurations | |
*/ | |
var servers = { | |
'www1.localhost': { | |
app: './www1/app.js', | |
http: { | |
port: 3010 | |
}, | |
https: { | |
credentials: crypto.createCredentials({ | |
key: fs.readFileSync('certs/www1.localhost.key'), | |
cert: fs.readFileSync('certs/www1.localhost.crt') | |
}) | |
} | |
}, | |
'www2.localhost': { | |
app: './www2/app.js', | |
http: { | |
port: 3011 | |
}, | |
https: { | |
credentials: crypto.createCredentials({ | |
key: fs.readFileSync('certs/www2.localhost.key'), | |
cert: fs.readFileSync('certs/www2.localhost.crt') | |
}) | |
} | |
} | |
} | |
// | |
// DO NOT EDIT BELOW | |
// | |
// Http proxy. | |
var proxy = new httpProxy.RoutingProxy(); | |
/* | |
* Boot our applications. | |
*/ | |
Object.keys(servers).forEach(function(serverName) { | |
var server = servers[serverName]; | |
require(server.app).app.listen(server.http.port ? server.http.port : server.https.port); | |
}); | |
/* | |
* Listen for http requests. | |
*/ | |
http.createServer(function (req, res) { | |
var host = req.headers.host.match(/(.*):[^:]+$/)[1]; | |
if (servers[host] && servers[host].http) { | |
proxy.proxyRequest(req, res, {host: host, port: servers[host].http.port}); | |
} | |
else { | |
res.send(400); | |
} | |
}).listen(3000); | |
/* | |
* Listen for https requests. | |
*/ | |
https.createServer( | |
{ | |
SNICallback: function(servername) { | |
// Check if we have https credentials for this user. | |
if (servers[servername] && servers[servername].https) { | |
return servers[servername].https.credentials.context; | |
} | |
return; | |
} | |
}, | |
function (req, res) { | |
var host = req.headers.host.match(/(.*):[^:]+$/)[1]; | |
if (servers[host] && servers[host].https) { | |
proxy.proxyRequest(req, res, {host: host, port: servers[host].https.port ? servers[host].https.port : servers[host].http.port}); | |
} | |
else { | |
res.send(400); | |
} | |
} | |
).listen(3001); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.app = (function() { | |
return Http.createServer(function (req, res) { | |
res.writeHead(200, { 'Content-Type': 'text/plain' }); | |
res.write('request successfully proxied 1: ' + req.url +'\n' + JSON.stringify(req.headers, true, 2)); | |
res.end(); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment