39

在进行服务器端渲染时出现以下错误。

RENDERING ERROR: { [Error: Network error: request to https://api-dev.xyz.io/graphql failed, reason: Hostname/IP doesn't match certificate's altnames: "Host: localhost. is not in the cert's altnames: DNS:*.xyz.io"]
  graphQLErrors: null,
  networkError: 
   { [FetchError: request to https://api-dev.xyz.io/graphql failed, reason: Hostname/IP doesn't match certificate's altnames: "Host: localhost. is not in the cert's altnames: DNS:*.xyz.io"]
     name: 'FetchError',
     message: 'request to https://api-dev.xyz.io/graphql failed, reason: Hostname/IP doesn\'t match certificate\'s altnames: "Host: localhost. is not in the cert\'s altnames: DNS:*.xyz.io"',
     type: 'system',
     errno: undefined,
     code: undefined },
  message: 'Network error: request to https://api-dev.xyz.io/graphql failed, reason: Hostname/IP doesn\'t match certificate\'s altnames: "Host: localhost. is not in the cert\'s altnames: DNS:*.xyz.io"',
  extraInfo: undefined }

注意:-我正在使用 react、redux、apollo-client(GraphQL) 和 ExpressJS(NodeJS)。我向其发出请求的 API 服务器位于另一个域中,我无法对其进行任何更改。

在使用客户端渲染时,我没有遇到任何困难,一切都按预期工作,但是在进行服务器端渲染时,我遇到了上述错误。

所以我在我的服务器上尝试了以下方法,但仍然没有运气。

  1. 添加自签名证书

  2. 在 https 选项中添加 'rejectUnauthorized':false。

    const options = {
      'key': key,
      'cert': cert,
      'ca': [ fs.readFileSync('local-certificate.pem') ],
      'rejectUnauthorized':false
    };
    
    https.createServer(options, app).listen(httpsPort, '0.0.0.0', function onStart(err) {
      if (err) { console.log(err); }
      console.info('==> Listening on httpsPort %s. Open up http://0.0.0.0:%s/ in your browser.', httpsPort, options);
    });
    
  3. 此外,我还尝试借助如何使用 OpenSSL 生成带有 SubjectAltName 的自签名证书?

有没有办法绕过证书验证,以便我的快速服务器可以向另一个域上具有有效证书的 API 服务器发出请求?

我仍然有点不确定是否可以通过在我的最后(在我的快速服务器上)进行任何更改来修复它。

请让我知道这方面的任何见解。

4

4 回答 4

1

我相信迈克尔兰迪斯试图表达这一点,但并没有完全提供解决方案。如果您的服务在 192.168.100.100 本地运行,那么这需要在您的 hosts 文件中(例如,Linux 上的 /etc/hosts):

192.168.100.100 api-dev.xyz.io

当前失败的请求需要发送到 api-dev.xyz.io,即使在 SSR 期间也是如此。避免对“http[s]://localhost/...”的请求要容易得多。

于 2022-02-09T03:57:44.033 回答
1

该错误不是由您的问题中的代码引起的。

您正在做的是创建一个新的 HTTP 服务器,它将侦听httpsPort并绑定到0.0.0.0这意味着所有本地 IP 地址。

我建议您在绑定到所有本地 IP 地址时省略0.0.0.0,因为无论如何这是默认行为,但这不是问题。

然后您为此服务器分配证书local-certificate.pem,这意味着连接到此服务器的客户端将获得此证书。

在这部分代码中,您没有连接到任何外部 API。

于 2022-01-13T18:06:23.683 回答
0

错误消息表明位于https://api-dev.xyz.io/graphql的服务器认为传入的请求被定向到 https://localhost/graphql。因此,它失败了,因为 SSL 证书没有保护 https://localhost/。

您的服务器端渲染是否会尝试对执行服务器端渲染的同一服务器执行 fetch/API 调用以加载渲染的初始数据?如果是这样,客户端需要从 API 获取的初始数据是否可以直接传递给服务器端渲染,而不是 React 尝试在服务器端渲染中对自身执行 API 调用?

也许本教程可以用作示例参考。

于 2021-04-10T12:40:38.930 回答
-2

主机名是 NodeJS 下面的系统级设置。您需要访问运行您的 NodeJS 副本的机器/VM。

于 2021-02-10T00:18:42.110 回答