I currently have a HTTPS web server listening on port 443 on my host machine.
My goal is to set up another HTTPS web server on the same host machine, change ports on both web servers, and then set up a proxy server using node-http-proxy listening on port 443 instead. The proxy server then delegates requests based on custom logic to the servers on other ports.
Below is the proxy server I adapted from one I successfully use when proxying plain HTTP requests on port 80. However, when I try to run this code the browser displays the message 'Secure Proxy Server unable to handle your request at this time.' and console logs '[Error: UNABLE_TO_VERIFY_LEAF_SIGNATURE]' It does make it to the point where it tries to proxy the request to the server listening on a different port.
var sugar = require('sugar')
var url = require('url')
var https = require('https')
var httpProxy = require('http-proxy')
var fs = require('fs')
//configure proxy
var ssl proxy = httpProxy.createProxyServer({
ssl: {
key: fs.readFileSync('/cert/server.key', 'utf-8'),
cert: fs.readFileSync('/cert/mydomain.crt', 'utf-8')
}
})
sslproxy.on(
'error',
function(err, req, res) {
console.log(err)
res.end('Secure Proxy Server unable to handle your request at this time.')
}
)
//configure and start server that uses proxy
var credentials = {
key: fs.readFileSync('/cert/server.key','utf-8'),
cert: fs.readFileSync('/cert/mydomain.crt', 'utf-8')
}
var sslserver = https.createServer(
credentials,
function(req, res) {
console.log("Received request on secure proxy server")
var target = url.parse(req.url)
if(target.pathname.startsWith('/version1')) {
console.log("Forwarding request to port 444")
sslproxy.web(req, res, {target: 'https://localhost:444'})
} else {
console.log("Forwarding request to 445")
sslproxy.web(req, res, {target: 'https://localhost:445'})
}
}
)
sslserver.listen(443)
Couple thoughts:
- I tried using node-ssl-root-cas as indicated in the answer to another question, but nothing appeared to change. My SSL certificate is from Network Solutions.
- The targets for my proxy are localhost:444 and localhost:445 because those ports are not open externally and cannot be. Not sure if the localhost in the host name is affecting the https proxy.