Created
April 14, 2014 06:53
-
-
Save richardkazuomiller/10622473 to your computer and use it in GitHub Desktop.
node-http-proxy with https
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 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