node-http-proxy icon indicating copy to clipboard operation
node-http-proxy copied to clipboard

Proxy https to http (example) not working

Open 3dprogramin opened this issue 4 years ago • 2 comments
trafficstars

I have a HTTP web server and I'm trying to set it up behind a HTTPS proxy server. I've tried the example that's in the repository: https://github.com/http-party/node-http-proxy/blob/master/examples/http/proxy-https-to-http.js but I'm always getting ERR_EMPTY_RESPONSE in browser.

Example code

var https = require('https'),
    http  = require('http'),
    util  = require('util'),
    path  = require('path'),
    fs    = require('fs'),
    colors = require('colors'),
    httpProxy = require('../../lib/http-proxy'),
    fixturesDir = path.join(__dirname, '..', '..', 'test', 'fixtures');

//
// Create the target HTTP server
//
http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('hello http over https\n');
  res.end();
}).listen(9009);

//
// Create the HTTPS proxy server listening on port 8000
//
httpProxy.createServer({
  target: {
    host: 'localhost',
    port: 9009
  },
  ssl: {
    key: fs.readFileSync(path.join(fixturesDir, 'agent2-key.pem'), 'utf8'),
    cert: fs.readFileSync(path.join(fixturesDir, 'agent2-cert.pem'), 'utf8')
  }
}).listen(8009);

console.log('https proxy server'.blue + ' started '.green.bold + 'on port '.blue + '8009'.yellow);
console.log('http server '.blue + 'started '.green.bold + 'on port '.blue + '9009 '.yellow);

Error

screenshot

Does anybody know what's wrong ? Any suggestion is appreciated.

3dprogramin avatar Aug 21 '21 09:08 3dprogramin

Have you tried explicitly setting https:// in the url? So https://localhost:9009?

swapsCAPS avatar Nov 19 '21 13:11 swapsCAPS

After talking to my local neighborhood devOps wizard, this is most likely being caused by the browser not doing a TLS handshake, since it has no idea that the server you're trying to talk to is https (we're not connecting to port 443 and we did not set the correct scheme in the URL). So it just sends the http headers over the TCP stream immediately, the server (node-http-proxy) on the other hand expects a TLS handshake, so it just breaks and ends the stream. Maybe node-http-proxy should emit an error or warning event for this.

swapsCAPS avatar Nov 22 '21 10:11 swapsCAPS