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

Correct way to set up HTTPS proxy

Open djpimp2010 opened this issue 12 years ago • 4 comments

I have used your repo for a standard HTTP reverse proxy, and it works brilliantly (except a small issue with status codes), and I have been working on a HTTPS version.

I had to get code form another repo for SNI, and I have that successfully set up.

I can go to https://domain1.com:444 and https://domain2.com:444 and both will show the correct certificates, and the default certificates are set as domain1.com

This works brilliantly, and the correct certificate is always shown for the domain, but I cannot get the HTTPS script to use the new version of your API to proxy a request (proxy.web(req,res, {});)

Here is the server that currently proxies to a single target:

var certs = {
        'domain1.com': getCredentialsContext('domain1')
};

var options = {
              ssl: {
                SNICallback: function(hostname){
                  //console.log(certs[hostname]);
                  return certs[hostname];
                },
                key: fs.readFileSync('/etc/ssl/certs/default.key'),
                cert: fs.readFileSync('/etc/ssl/certs/default.crt'),
              },
              hostnameOnly: true,
              target: 'https://192.168.0.12:443',
              secure: false,
            }

var server = httpProxy.createServer(options);

console.log('listening 444');
server.listen(444);

As said above, this server works halfway, but can only proxy to a single target, After googling, i have noticed you removed the router: {} options

I have tried setting up like this: (but with ssl settings) https://github.com/nodejitsu/node-http-proxy/issues/594

If I set it up like the example in the above issue, then the server wont even listen for connections, although no errors are thrown.

What would be the correct syntax to change to multiple targets from the code above ? It looked so easy with the router options, I there any way of me botching some old code back in, to get the router options working again?

If not, is there a way I can pick the internal ip to proxy to, depending on the hostname used to access the proxy ?

Node.js has proved very very powerful, and it looks faster than a dedicated nginx reverse proxy !!

Thanks!

djpimp2010 avatar Mar 04 '14 10:03 djpimp2010

Did you ever have any luck setting this up? I am trying to figure how to do SNI with node-http-proxy as well.

lsvx avatar Feb 17 '15 17:02 lsvx

@lsvx i believe this issue is around the old API usage. My recommendation here is to create a standard HTTPS server (which uses SNI callback) and create a separate http-proxy instance to proxy the requests within the handler.

var https = require('https');
var HttpProxy = require('http-proxy');

// Intantiate a proxy that also changes the origin of the incoming request to the target host
var proxy = new HttpProxy({ changeOrigin: true });

var options = {}; // SSL OPTS AND SUCH with SNI callback

https.createServer(options, function (req, res) {
  proxy.web(req, res, {
    target: 'https://whatever.com'
  });
});

Please create a new issue if this does not work out for you as this issue itself is dated. Thanks!

jcrugzz avatar Feb 17 '15 17:02 jcrugzz

@jcrugzz,

Thanks for your example! I was able to get SNI working perfectly.

lsvx avatar Feb 18 '15 00:02 lsvx

@jcrugzz Minor and clever solution!

lekotros avatar Feb 18 '15 07:02 lekotros