Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple HTTP->HTTPS Proxy Example? #304

Closed
cweichen opened this issue Sep 1, 2012 · 11 comments
Closed

Simple HTTP->HTTPS Proxy Example? #304

cweichen opened this issue Sep 1, 2012 · 11 comments

Comments

@cweichen
Copy link

cweichen commented Sep 1, 2012

Hi,

I'm a node/node-http-proxy novice, and I'd like to use node-http-proxy to create an HTTP->HTTPS proxy server. I.e. a client would connect to the proxy over HTTP, and on the back-end it would talk to an HTTPS target web server. The two examples given are for HTTPS->HTTP and HTTPS->HTTPS, but not HTTP->HTTPS.

Maybe it's obvious from these examples how to do it, but I'm not able to figure it out.

Here's a sample of my code:

var httpProxy = require('http-proxy');

httpProxy.createServer(function (req, res, proxy) {
  proxy.proxyRequest(req, res, {
    host: 'secure.target.host',
    port: 443,
    https: true
  });
}).listen(8000);

When I try to connect to http://localhost:8000 I get a 500 Internal Server Error with the body

An error has occurred: {"code":"ECONNRESET"}

If anyone can show me what I'm doing wrong, that would be greatly appreciated! Thanks!

@cweichen
Copy link
Author

cweichen commented Sep 3, 2012

Never mind, I figured it out. I just had to ignore all the extraneous code in the HTTPS->HTTPS example. This will do it:

var httpProxy = require('http-proxy');

var options = {
    target: {
        https: true
    }
}

httpProxy.createServer(443, 'secure.target.host', options).listen(8000);

Beautiful! Thanks node-http-proxy!

@cweichen cweichen closed this as completed Sep 3, 2012
@aaronshaf
Copy link

This doesn't work for me. I get:

An error has occurred: {"code":"ECONNRESET"}

(I have tried different hosts, of course)

See also: http://stackoverflow.com/questions/15801014/how-to-use-node-http-proxy-for-http-to-https-routing

Update: This did the trick:

https://gist.github.com/aaronshaf/6469450

@sheldonkwok
Copy link

Can you repost your gist? :)

@kirkedev
Copy link

Figured this out myself after tearing my hair out a bit. The above solution wouldn't work for me because I needed to inspect the request before proxying it. You need to place the https: true option inside a target object.

require('http-proxy').createServer(function (req, res, proxy) {
  // Inspect request and decide whether to proxy, then...
  proxy.proxyRequest(req, res, {
    host: 'secure.target.host',
    port: 443,
    target: {
       https: true
    }
  });
}).listen(8080);

@jasonsanjose
Copy link

There's an example in the examples folder now: https://github.com/nodejitsu/node-http-proxy/blob/caronte/examples/http/proxy-http-to-https.js

httpProxy.createProxyServer({
  target: 'https://google.com',
  agent  : https.globalAgent,
  headers: {
    host: 'google.com'
  }
}).listen(8011);

Worked for me.

@ericmdantas
Copy link

In case someone's having problem with it, I've created a simple working repo: https://github.com/ericmdantas/simple-node-https-proxy

@vali-pavel
Copy link

@ericmdantas I'm trying to use your repo to access secured web pages, but it seems to not be working because I get Secure Connection Failed in the web page...

@ericmdantas
Copy link

@Valeriu92, when I access https://localhost:9998/api/server2 I get the payload correctly: {"msg2":"came from server2"}

@suculent
Copy link

It works for me only with secure: false in options which is unfortunate, becauase the target server has valid letsencrypt certificate. How do I debug the SSL layer verbosely to see why is the 'first certificate' invalid?

@rickysahu
Copy link

I had to use these options

{
  secure: false,
  target: 'https://localhost:443',
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants