3

I have a reverse proxy on my endpoint like this:

var express = require('express');
var app = express();
var httpProxy = require('http-proxy');
var apiProxy = httpProxy.createProxyServer();
var serverOne = 'https://idena.navarra.es/ogc/wms?';

app.all('/idena', function (req, res) {
  apiProxy.web(req, res, {target: serverOne});
});

app.listen(3000, function () {
  console.log('Working!');
});

When a request to /idena is received, the server throws an exception like this:

Error [ERR_TLS_CERT_ALTNAME_INVALID]: Hostname/IP does not match certificate's altnames: Host: localhost. is not in the cert's altnames: DNS:idena.navarra.es, DNS:www.idena.navarra.es at Object.checkServerIdentity (tls.js:235:17) at TLSSocket.onConnectSecure (_tls_wrap.js:1061:27) at TLSSocket.emit (events.js:189:13) at TLSSocket._finishInit (_tls_wrap.js:633:8)

How can I solve this? Guess is due to https but no idea of how to avoid that, thanks!

Iñigo
  • 1,877
  • 7
  • 25
  • 55
  • Check [**how to use https**](https://www.npmjs.com/package/http-proxy#using-https) with this package – codtex Sep 03 '19 at 06:43
  • Possible duplicate of [Node.js Hostname/IP doesn't match certificate's altnames](https://stackoverflow.com/questions/14262986/node-js-hostname-ip-doesnt-match-certificates-altnames) – Charlie Sep 03 '19 at 06:46

1 Answers1

11

Although the error is about mismatching SSL certificate and domain names, in http-proxy module, the error often manifests when your server is HTTP and the Target is HTTPS.

You can avoid this error through the change changeOrigin flag.

const proxy = httpProxy.createProxyServer();

proxy.web(req, res, {
  changeOrigin: true,
  target: https://example.com:3000,
});

In case your server is HTTPS and target one is HTTPS as well, you should include SSL certificate

httpProxy.createServer({
  ssl: {
    key: fs.readFileSync('valid-ssl-key.pem', 'utf8'),
    cert: fs.readFileSync('valid-ssl-cert.pem', 'utf8')
  },
  target: 'https://example.com:3000',
  secure: true
}).listen(443);

Please see this question.

Charlie
  • 22,886
  • 11
  • 59
  • 90