2

I have an application that creates a proxy server and returns the url requests when a page is accessed, but it works only for http web pages and when I'm trying to access a https address then I get Secure Connection Failed in the browser.

To solve this, I generated a Self-Signed Certificate for localhost:8080 from here, but still can't access secured web pages...

This is my code:

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

var proxy = httpProxy.createServer({
  ssl: {
    key:  fs.readFileSync('ssl_key_8080.pem', 'utf8'),
    cert:  fs.readFileSync('ssl_cert_8080.pem', 'utf8')
  },
  target:'https://localhost:8080',
  secure: true
});

proxy.listen(443);

var http = require('http');

http.createServer(function (req, res) {
  var options = {
    target: 'http://' + req.headers.host,
  };
  req.host = req.headers.host;
  proxy.web(req, res, options, function(err){
    console.log('err', err)
  });
}).listen(8080);

proxy.on('proxyReq', function (proxyReq, req, res) {
  console.log('request url', JSON.stringify(req.url, true, 2));
});

Is there something that I'm not doing right? I followed the instructions from http-proxy docs

1 Answer 1

2

The issue is you have a self signed certificate and you are using the secure flag in the proxy settings object, from the docs

You can activate the validation of a secure SSL certificate to the target connection (avoid self signed certs), just set secure: true in the options.

    var proxy = httpProxy.createServer({
  ssl: {
    key:  fs.readFileSync('ssl_key_8080.pem', 'utf8'),
    cert:  fs.readFileSync('ssl_cert_8080.pem', 'utf8')
  },
  target:'https://localhost:8080',
  secure: true
});

If you remove the secure flag, you might get an error in your browser that the route isn't safe.

In the context of your code.

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

var proxy = httpProxy.createServer({
  ssl: {
    key:  fs.readFileSync('ssl_key_8080.pem', 'utf8'),
    cert:  fs.readFileSync('ssl_cert_8080.pem', 'utf8')
  },
  target:'https://localhost:8080'
});

proxy.listen(443);

var http = require('http');

http.createServer(function (req, res) {
  var options = {
    target: 'http://' + req.headers.host,
  };
  req.host = req.headers.host;
  proxy.web(req, res, options, function(err){
    console.log('err', err)
  });
}).listen(8080);

proxy.on('proxyReq', function (proxyReq, req, res) {
  console.log('request url', JSON.stringify(req.url, true, 2));
});
Sign up to request clarification or add additional context in comments.

7 Comments

Still not working, now I get The connection was reset in the browser
Are you running node as root? This explains why it is needed to listen on port 443 stackoverflow.com/questions/26579028/…
You can either run as root or you can change line 13 to listen on a different port
Yes, I'm running node as root with sudo node
Have you got the logs from the node process. I've put it up on Glitch if you want to play around on there? glitch.com/edit/#!/join/8e40de60-f8e9-4400-837d-8d87d4151cbd
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.