Skip to content

Instantly share code, notes, and snippets.

@richardkazuomiller
Created April 14, 2014 06:53
Show Gist options
  • Save richardkazuomiller/10622473 to your computer and use it in GitHub Desktop.
Save richardkazuomiller/10622473 to your computer and use it in GitHub Desktop.
node-http-proxy with https
var httpProxy = require('http-proxy')
var fs = require('fs');
var proxy = httpProxy.createProxy({
ws : true,
});
var ssl = {
key: fs.readFileSync('server_nopass.key', 'utf8'),
cert: fs.readFileSync('server.crt', 'utf8')
}
var options = {
'hackathon.dev': 'http://0.0.0.0:9006',
'turkeyflip.dev': 'http://0.0.0.0:9008',
'picshop.dev' : 'http://0.0.0.0:3000',
'api.nailly.jp' : 'https://0.0.0.0:8081',
'default' : 'http://0.0.0.0:9008'
}
var server = require('https').createServer(ssl, function(req, res) {
req.url = req.url.replace('/rickyedit','')
req.headers['X-Forwarded-Proto'] = 'https'
proxy.web(req, res, {
target: options[req.headers.host]||options.default
},function(e){
log_error(e,req);
});
})
server.on('upgrade',function(req,res){
proxy.ws(req, res, {
target: options[req.headers.host]||options.default
},function(e){
log_error(e,req);
});
})
server.listen(443)
function log_error(e,req){
if(e){
console.error(e.message);
console.log(req.headers.host,'-->',options[req.headers.host]);
console.log('-----');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment