NodeJS Express 反向代理 TLS_CERT_ALTNAME_INVALID 错误

NodeJS Express reverse proxy TLS_CERT_ALTNAME_INVALID error

我的端点上有一个反向代理,如下所示:

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!');
});

当收到对/idena的请求时,服务器会抛出这样的异常:

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)

我该如何解决这个问题?猜测是由于 https 但不知道如何避免,谢谢!

虽然是SSL证书和域名不匹配的错误,但是在http-proxy模块中,当你的服务器是HTTP,Target是HTTPS时,这个错误经常出现。

您可以通过更改 changeOrigin 标志来避免此错误。

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);

请参阅this问题。