【问题标题】:Node.js IPv6 issueNode.js IPv6 问题
【发布时间】:2019-12-03 02:32:20
【问题描述】:

Node.js 服务器部署在 Google Cloud Platform 上,服务器未监听 ipv6。 当我在移动浏览器 (chrome) 中运行我的 API 时,我看到此错误:此站点无法提供安全连接。 ERR_SSL_PROTOCOL_ERROR

如何让 node.js 服务器在 ipv6 上运行? 以下是我的代码:

const app = express();
const port = 8080;

const privateKey = fs.readFileSync(
  '/path/to/privkey.pem',
  'utf8'
);
const certificate = fs.readFileSync(
  '/path/to/cert.pem',
  'utf8'
);
const ca = fs.readFileSync(
  '/path/to/chain.pem',
  'utf8'
);

const options = {
  key: privateKey,
  cert: certificate,
  ca: ca
};

https.createServer(options, app).listen(port, '::', () => {
    console.log(`Server started on port ${port}`);
  });

【问题讨论】:

  • 您在哪个服务上运行此代码?只有某些 Google 托管服务支持 IPv6。此外,您的代码正在尝试在非标准端口 (8080) 上设置 SSL。
  • 您使用的是 Compute Engine 还是 App Engine?
  • 我正在使用计算引擎。如果我使用 HTTP(端口 80)创建服务器,那么它在 IPv6 上运行,但我不能将 IPv6 与 HTTPS 一起使用。
  • 请仔细检查您是否设置了 LoadBalancer 以在 GCP 中的实例上使用 ipv6? cloud.google.com/load-balancing/docs/ipv6
  • 是的,我们已经配置了一个负载均衡器来使用 IPv6。

标签: node.js express ssl google-cloud-platform ipv6


【解决方案1】:

我建议你试试这段代码。你应该可以在 localhost 的 8082 端口中检查它:https://localhost:8082/

const https = require('https');
const fs = require('fs');

const options = {
  key: fs.readFileSync('key.pem'),
  cert: fs.readFileSync('cert.pem')
};

https.createServer(options, function (req, res) {
  res.writeHead(200);
  res.end("hello world\n");
}).listen(8082);

希望这会有所帮助。

【讨论】:

  • 这不起作用。如果有人对此问题有所了解,请分享另一个解决方案。
  • 我可以通过在端口 80 上使用 HTTP 创建服务器来使用 IPv6,但 IPv6 不能使用 HTTPS。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-04
  • 2013-12-09
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多