NodeJS Express reverse proxy TLS_CERT_ALTNAME_INVALID error
Asked Answered
B

1

5

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!

Bengal answered 3/9, 2019 at 6:29 Comment(2)
Check how to use https with this packageDoubly
Possible duplicate of Node.js Hostname/IP doesn't match certificate's altnamesHugohugon
H
13

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.

Hugohugon answered 3/9, 2019 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.