【发布时间】:2020-11-29 07:19:00
【问题描述】:
我在 App Engine 上有一个常规的 Node(v12) http 服务器。我用http2.createSecureServer 切换到http2。它在开发中工作,但部署到 App Engine 并在服务器成功启动后响应带有 502 bad gateway 的请求...
我尝试切换到http2.createServer 以不使用 https,并且请求从未收到响应(永远加载)。该请求在 App Engine 日志资源管理器中的最后一次日志显示:
[error] 27#27: *2 upstream sent no valid HTTP/1.0 header
while reading response header from upstream, client:
169.254.1.1, server: _, request: "GET / HTTP/1.1",
upstream: "http://127.0.0.1:8081/"
它似乎以某种方式期待 http1,但我不知道为什么。我也不知道为什么在8081端口,我把端口设置为8080。
花了一天的时间在 Google 和他们的文档中搜索有关将 http2 与 App Engine 结合使用的任何内容后,我已经筋疲力尽了。而且,他们的支持页面上写着“在 Stack Overflow 上发帖”..
main.js
const http2 = require('http2');
const fs = require('fs');
const app = new (require('koa'))();
const logger = require('koa-logger');
const bodyParser = require('koa-bodyparser');
const json = require('koa-json');
const cors = require('@koa/cors');
const router = require('./router.js');
const { PORT, HOST, KEY, CERT } = require('./config.js');
app.use(logger());
app.use(bodyParser());
app.use(json());
app.use(cors({ exposeHeaders: 'authorization' }));
app.use(router.routes());
app.use(router.allowedMethods());
const server = http2
.createSecureServer(
{
key: fs.readFileSync(KEY),
cert: fs.readFileSync(CERT),
allowHTTP1: true
},
app.callback()
)
.listen(PORT, () => {
console.log(`Koa HTTP/2 running at https://${HOST}:${PORT}`);
});
app.yaml
runtime: nodejs12
service: api
handlers:
- url: /.*
script: auto
secure: always
redirect_http_response_code: 301
vpc_access_connector:
name:
env_variables:
NODE_ENV: production
PORT:8080
KEY: key.pem
CERT: cert.pem
...
【问题讨论】:
-
发布您的 app.yaml 和源代码。
-
@JohnHanley 我添加了相关源码
-
SSL (TLS) 由 Google 前端 (GFE) 为您处理。 GFE 和您的应用程序之间的通信是端口 8080 上的 HTTP。一旦您更正代码以仅使用 HTTP,请更新您的问题。建议。测试时不要使用重定向 301。
-
HTTP/2 需要 SSL,不是吗?因此,如果 GFE 和 Node 服务器之间的通信是 HTTP,那么 HTTP/2 将被转换,我不会从使用它中受益。对吗?
-
那么您就不能使用 App Engine。如果您想控制 TCP/IP 堆栈,则需要使用 Compute Engine。