我正在尝试部署一个启用了 HTTP2(H2) 的简单 NodeJS 应用程序,这在本地运行良好
这是我的 NodeJS 代码
const spdy = require('spdy')
const express = require('express')
const path = require('path')
const fs = require('fs')
const port = 8080
const CERT_DIR = `${__dirname}/cert`;
const app = express()
app.get('*', (req, res) => {
res
.status(200)
.json({message: 'Welcome to node HTTP2'})
})
const options = {
key: fs.readFileSync(`${CERT_DIR}/server.key`),
cert: fs.readFileSync(`${CERT_DIR}/server.crt`)
}
spdy
.createServer(options, app)
.listen(port, (error) => {
if (error) {
console.error(error)
} else {
console.log('Listening on port: ' + port + '.')
}
})
我已经使用
openssl
命令创建了自签名证书和密钥,这是本地浏览器中的输出
但是在我将其部署到云后运行并启用端到端使用 HTTP/2,如下所示。
它给了我以下回复
我检查了cloud run的日志,错误信息是
The request failed because either the HTTP response was malformed or connection to the instance had an error. Additional troubleshooting documentation can be found at: https://cloud.google.com/run/docs/troubleshooting#malformed-response-or-connection-error
如果你问我,为什么我要把我的应用程序变成HTTP2,那是因为我想要发出大小超过32MB的请求,如果你使用HTTP/1服务器,这是云运行的限制
我的问题是,是否可以部署我的 NodeJS 应用程序并启用 HTTP/2 服务器以进行云运行?如果是的话我上面到底哪里出了问题?
基于此文档使用 HTTP/2(服务):
您的 Cloud Run 服务必须处理 HTTP/2 明文 (
) 格式的请求,因为 TLS 仍会由 Cloud Run 自动终止。h2c
要确认您的服务支持
h2c
请求,请使用以下 cURL 命令在本地测试服务:
curl -i --http2-prior-knowledge http://localhost:[PORT]
让我知道这是否有效。