【问题标题】:Host: localhost. is not in the cert's altnames主机:本地主机。不在证书的替代名称中
【发布时间】:2017-06-27 06:54:11
【问题描述】:

我在进行服务器端渲染时遇到以下错误。

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. 我还尝试在How can I generate a self-signed certificate with SubjectAltName using OpenSSL? 的帮助下在我的自签名证书中添加一个替代名称

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

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

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

【问题讨论】:

  • 我确实遇到了同样的问题,我从 apollo-boost 迁移这里是链接:apollographql.com/docs/react/migrating/boost-migration 并且它有效
  • 我遇到了同样的问题。你找到解决办法了吗?
  • 我建议您更好地改写/解释您的问题。如果可能,请使用图表来解释服务器/客户端部分。您可以创建证书,也可以编辑 hosts 文件,但在我给出答案之前,我需要更好地理解您的问题。

标签: node.js reactjs ssl-certificate graphql react-apollo


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    也许this tutorial 可以用作示例参考。

    【讨论】:

      猜你喜欢
      • 2017-05-15
      • 2020-10-03
      • 1970-01-01
      • 2021-07-07
      • 2019-02-07
      • 2012-12-25
      相关资源
      最近更新 更多